c++ - 如何将队列元素插入 vector 中?

标签 c++ stl

我有

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

现在我想做的是在运行时定义 5 个队列并将数据放在这些队列上

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

等等

我让它编译,但它立即核心转储。我想我需要首先将队列实际添加到 vector 中(它们不会像 map 那样自动创建)。如何添加队列而不是指向队列的指针?

编辑:

我真的应该指出我正在使用的队列类不是 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());

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

关于c++ - 如何将队列元素插入 vector 中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4065847/

相关文章:

c++ - 如何将命令行参数转换为 int?

c++ - 使用CMake制作后如何将目录内容复制到构建目录中?

c++ - 使用 vector (STL)时的未定义行为,请解释以下代码输出背后的原因

c++ - 如何处理多个迭代器类型

c++ - QTime 向新对象添加秒数

c++ - 使用 OpenCV 2.4 在 MFC SDI View 或 Control 中加载图像

c++ - 减少STL vector 的容量

c++ - 以相反的顺序插入 vector

c++ - CMenu::ModifyMenu 的问题

c++ - 基于堆栈缓冲区的STL分配器?