2013-02-02 102 views
1

队列类结构,我更多的是硬件的人,但芯片设计工具,我使用的是要求我写一些C++代码。我不熟悉面向对象的编程;虽然我有C.办理好什么,我要求的是如何构建我的类(称为cq)来完成手头任务的说明。可变尺寸和类型

我想能够生成一个指定的数据类型和指定的大小(生成后应该不会改变)的队列。理想情况下,这将是这样做...

my_queue = new cq(uint8_t, 6); 

...这将生成六个8位无符号整数的数组(或向量)。

然后,我想的方法既插入一个元素到一个端部和在所述队列的头返回元素如下。

uint8_t front; 
front = my_queue.advance(28); 

我需要什么样的结构来完成这个?我需要的模板,因为数据类型是可变的?或者我应该有一个泛型类,并且每个数据类型都有一个类继承它的结构?

谢谢!

编辑:使用从下面的答案输入,我已经想出了以下内容:

template <class type> 
template <class size> 
class CQ { 

    private: 
     // Allocates a queue with type and size 
     // specified by template arguments. 
     std::queue<type> cycle_queue(size, 0); 

    public: 
     // Inserts input to the end of the queue; 
     // removes and returns the front element. 
     type advance(type in){ 
      type out = cycle_queue.front(); 
      cycle_queue.push_back(in); 
      cycle_queue.pop_front(); 
      return out; 
     } 

} 

我的问题就变成了......我怎么在我的主C++程序实例呢?再次

CQ<uint8_t><6> my_queue; 
my_queue.advance(28); 

感谢:我想这一点,但它没有工作!

回答

1

这看起来像是一个完美的应用程序STL containers.您可以编写自己的队列类(作为模板类,因为您希望能够指定数据类型),但为什么重新发明轮子?

您正在查找的可能是:std::list,用于FIFO队列。为了您的具体的例子:

std::list<uint8_t> my_queue; 
my_queue.push_back(28);   // push an element 
uint8_t front = my_queue.front(); // get the element on the front of the queue 
my_queue.pop_front();    // and then pop it 

如果您还不有点熟悉OOP和C++,编写自己的模板类可能会有点遥不可及的现在。尽管如果你想尝试:例如,网络上有很多参考资料http://www.cplusplus.com/doc/tutorial/templates/

+0

这似乎确实很有吸引力;然而,这种解决方案是否保持恒定的队列大小?当我开始时,我不希望它是空的。我希望它始终具有我指定的长度并初始化为零。 –

+0

当你从这个容器中移除元素时会发生什么? – Spook

+0

@EvanW:所以你想要一个固定大小的铃声? – sheu

1

尝试:

#include <cstdio> 
#include <queue> 

int main(int argc, char * argv[]) 
{ 
    std::deque<int> myQueue; 

    myQueue.push_back(1); 
    myQueue.push_back(2); 
    myQueue.push_back(3); 

    for (int i = 0; i < 3; i++) 
    { 
     int j = myQueue.front(); 
     printf("%d ", j); 
     myQueue.pop_front(); 
    } 

    getchar(); 
} 


编辑:在回应评论

最简单的解决方案,这使我的头脑:

myQueue.push_back(newValue); 
while (myQueue.size > 6) 
    myQueue.pop_front(); 

事实上,您可以很容易地将这些代码包装在你自己的类中,例如:

template <class T> 
class SixItemsQueue 
{ 
private: 
    std::deque<T> data; 

public: 
    void Push(T value) 
    { 
     data.push_back(value); 
     while (data.size() > 6) 
      data.pop_front(); 
    } 

    T Pop() 
    { 
     T result = data.front(); 
     data.pop_front(); 
     return result; 
    } 
}; 
+0

这看起来很有吸引力;然而,这种解决方案是否保持恒定的队列大小?当我开始时,我不希望它是空的。我希望它始终具有我指定的长度并初始化为零。 –

+0

为什么你想要它的大小不变? std :: deque以这种方式实现,尽可能高效地添加和删除元素。 – Spook

+0

我需要这个队列来存储输入,以便我可以比较要测试的电路的输出和前一个周期的输入。例如,如果我的电路接收输入并在6个周期后生成有效输入,则需要一个长度为6的队列来存储最后六个周期的输入。当元素出队时,我可以将其与输出进行比较,因为我知道这是我六个周期前的输入。 –

2

考虑使用stl containers,像std::vector(这里:http://www.cplusplus.com/reference/vector/vector/)或std::list(这里:http://www.cplusplus.com/reference/list/list/)。这些集合已经做到了你想要达到的目的,你的类只需要实现对该集合的访问器。

原型应该是这样的:

std::vector Variable<uint8_t>; 

或者,你需要使用Templates。对他们的工作,他们是什么以及如何全面的解释可以在这里找到:http://www.cplusplus.com/doc/tutorial/templates/

在本质上,你将与

cq<uint8_t>(6); 

声明你的对象,并在构造函数中你会把:

template <class T> 
cq::cq(int amount) { 
    Buffer = new T[amount]; 
} 

请不要忘记在您使用'free'完成后释放内存。