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

发布于 2024-09-29 15:33:54 字数 3045 浏览 0 评论 0原文

现在我

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< /a>

我没想到编译器会抱怨它,否则我会预先声明它。

不幸的是,当我编译应用程序时,我收到此编译错误。

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

I have

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

Now what I want to do is define at runtime say 5 queues and place data on those queues like

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

etc.

I got it to compile, but it core dumps right away. I guess I need to actually add the queues to the vector first (they don't get created automatically like a map). How do I add a queue without it being a pointer to a queue?

EDIT:

I should really have pointed out that the queue class I am using is not std::queue but this one: http://www.justsoftwaresolutions.co.uk/threading/implementing-a-thread-safe-queue-using-condition-variables.html

I didn't expect the compiler to complain about it otherwise I would have stated it up front.

Unfortunately when I compile the application I get this compilation error.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

如果没有你 2024-10-06 15:33:54

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

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

You have not allocated any of the 5 queues. Try:

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

Also not that std::vector uses indices starting from 0, so to insert into the first queue:

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

Follow-up to your edit: The concurrent queue does not seem to have a copy constructor. This means you can not put it in any standard container because it does not satisfy the requirements. The container's value-type must be copy-constructible and copy-assignable.

伤感在游骋 2024-10-06 15:33:54

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

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

You have to allocate memory for the five queues:

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

or

inp_queue.resize(5,InputQueue());

Now, you can do:

inp_queue[1].push_back(new MyObject);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文