如何将向量中插入队列元素? [英] How do I insert queue elements into a vector?

查看:198
本文介绍了如何将向量中插入队列元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有

  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 
在/ usr / include /c++/4.4/deque:63,
来自/usr/include/c++/4.4/queue:61,
来自concurrentqueue.h:3,
来自test.cpp:1:
/usr/include/c++/4.4/bits/stl_construct.h:在函数'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:实例化为'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:实例化为'void std :: uninitialized_fill_n(_ForwardIterator,_Size,const _Tp&)[with _ForwardIterator = concurrent_queue& > *,_Size = long unsigned int,_Tp = concurrent_queue< Packet *>]
/usr/include/c++/4.4/bits/stl_uninitialized.h:318:实例化为'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& *]'
/usr/include/c++/4.4/bits/stl_vector.h:1035:从'void std :: vector< _Tp,_Alloc> :: _ M_fill_initialize(size_t,const _Tp&与_Tp = concurrent_queue< Packet *>,_Alloc = std :: allocator< concurrent_queue< Packet *> >]'
/usr/include/c++/4.4/bits/stl_vector.h:230:从'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:从这里实例化
/usr/include/c++/4.4/bits/stl_construct.h:74:错误:没有匹配函数调用'concurrent_queue< Packet *> :: concurrent_queue(const concurrent_queue< Packet *>&)'
concurrentqueue.h:12:note:候选者是:concurrent_queue< Packet *> :: concurrent_queue()
concurrentqueue。 h:12:note:concurrent_queue< Packet *> :: concurrent_queue(concurrent_queue< Packet *&)


解决方案

您尚未分配任何5个队列。尝试:

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

也不是 std :: vector 从0开始的索引,因此插入到第一个队列:

  inp_queue [0] .push(new MyObject 

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


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

解决方案

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.

这篇关于如何将向量中插入队列元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆