如何将队列元素æ’入到矢é‡ä¸­ï¼Ÿ

时间:2010-10-31 23:59:00

标签: c++ stl

我有

typedef std::queue<MyObject*> InputQueue;
std::vector<InputQueue> inp_queue;

现在我è¦åšçš„是在è¿è¡Œæ—¶å®šä¹‰5个队列,并将数æ®æ”¾åœ¨è¿™äº›é˜Ÿåˆ—上,如

inp_queue[1].push(new MyObject());

ç­‰

我让它编译,但它立å³æ ¸å¿ƒè½¬å‚¨ã€‚我想我需è¦é¦–先将队列添加到å‘é‡ä¸­ï¼ˆå®ƒä»¬ä¸ä¼šåƒåœ°å›¾ä¸€æ ·è‡ªåŠ¨åˆ›å»ºï¼‰ã€‚如何在没有队列指针的情况下添加队列?

编辑:

我应该指出我正在使用的队列类ä¸æ˜¯std :: queue,而是这个:http://www.justsoftwaresolutions.co.uk/threading/implementing-a-thread-safe-queue-using-condition-variables.html

我没想到编译器会抱怨它,å¦åˆ™æˆ‘会事先说出æ¥ã€‚

ä¸å¹¸çš„是,当我编译应用程åºæ—¶ï¼Œæˆ‘得到了这个编译错误。

g++ test.cpp
In file included from /usr/include/c++/4.4/deque:63,
                 from /usr/include/c++/4.4/queue:61,
                 from concurrentqueue.h:3,
                 from test.cpp:1:
/usr/include/c++/4.4/bits/stl_construct.h: In function ‘void std::_Construct(_T1*, const _T2&) [with _T1 = concurrent_queue<Packet*>, _T2 = concurrent_queue<Packet*>]’:
/usr/include/c++/4.4/bits/stl_uninitialized.h:187:   instantiated from ‘static void std::__uninitialized_fill_n<<anonymous> >::uninitialized_fill_n(_ForwardIterator, _Size, const _Tp&) [with _ForwardIterator = concurrent_queue<Packet*>*, _Size = long unsigned int, _Tp = concurrent_queue<Packet*>, bool <anonymous> = false]’
/usr/include/c++/4.4/bits/stl_uninitialized.h:223:   instantiated from ‘void std::uninitialized_fill_n(_ForwardIterator, _Size, const _Tp&) [with _ForwardIterator = concurrent_queue<Packet*>*, _Size = long unsigned int, _Tp = concurrent_queue<Packet*>]’
/usr/include/c++/4.4/bits/stl_uninitialized.h:318:   instantiated from ‘void std::__uninitialized_fill_n_a(_ForwardIterator, _Size, const _Tp&, std::allocator<_Tp2>&) [with _ForwardIterator = concurrent_queue<Packet*>*, _Size = long unsigned int, _Tp = concurrent_queue<Packet*>, _Tp2 = concurrent_queue<Packet*>]’
/usr/include/c++/4.4/bits/stl_vector.h:1035:   instantiated from ‘void std::vector<_Tp, _Alloc>::_M_fill_initialize(size_t, const _Tp&) [with _Tp = concurrent_queue<Packet*>, _Alloc = std::allocator<concurrent_queue<Packet*> >]’
/usr/include/c++/4.4/bits/stl_vector.h:230:   instantiated from ‘std::vector<_Tp, _Alloc>::vector(size_t, const _Tp&, const _Alloc&) [with _Tp = concurrent_queue<Packet*>, _Alloc = std::allocator<concurrent_queue<Packet*> >]’
test.cpp:18:   instantiated from here
/usr/include/c++/4.4/bits/stl_construct.h:74: error: no matching function for call to ‘concurrent_queue<Packet*>::concurrent_queue(const concurrent_queue<Packet*>&)’
concurrentqueue.h:12: note: candidates are: concurrent_queue<Packet*>::concurrent_queue()
concurrentqueue.h:12: note:                 concurrent_queue<Packet*>::concurrent_queue(concurrent_queue<Packet*>&)

2 个答案:

答案 0 :(得分:3)

您尚未分é…5个队列中的任何一个。å°è¯•ï¼š

typedef std::queue<MyObject*> InputQueue;
std::vector<InputQueue> inp_queue(5);
inp_queue[1].push(new MyObject());

åŒæ ·ä¸æ˜¯std::vector使用从0开始的索引,所以è¦æ’入第一个队列:

inp_queue[0].push(new MyObject());

编辑的åŽç»­è¡ŒåŠ¨ï¼šå¹¶å‘队列似乎没有å¤åˆ¶æž„造函数。这æ„味ç€æ‚¨æ— æ³•å°†å…¶æ”¾åœ¨ä»»ä½•æ ‡å‡†å®¹å™¨ä¸­ï¼Œå› ä¸ºå®ƒä¸æ»¡è¶³è¦æ±‚。容器的value-type必须是å¯å¤åˆ¶æž„造和å¯å¤åˆ¶çš„。

答案 1 :(得分:0)

您必须为五个队列分é…内存:

for (size_t queue_num=0;queue_num<5;++queue_num)
  inp_queue.push_back(InputQueue());

或

inp_queue.resize(5,InputQueue());

现在,你å¯ä»¥è¿™æ ·åšï¼š

inp_queue[1].push_back(new MyObject);