繁体   English   中英

如何将队列元素插入向量?

[英]How do I insert queue elements into a vector?

我有

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*>&)

您尚未分配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());

编辑的后续步骤 :并发队列似乎没有副本构造函数。 这意味着您不能将其放入任何标准容器中,因为它不能满足要求。 容器的值类型必须是可复制构造的和可复制分配的。

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

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);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM