2010-10-31 53 views
1

如何將隊列元素插入到矢量中?

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

現在我想做的是定義在運行時說,5隊列和地方數據對像隊列

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

我得到它來編譯,但它的核心轉儲馬上。我想我需要先將隊列添加到矢量中(它們不會像地圖一樣自動創建)。如何添加一個隊列而不將它作爲隊列的指針?

編輯:

我真的應該指出,隊列類我使用的是不是標準::隊列,但是這一個: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*>&) 
+0

編輯我的答案,以反映您的編輯。 – 2010-11-01 02:35:53

回答

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

後續的編輯:併發隊列似乎不有一個複製構造函數。這意味着您不能將它放在任何標準容器中,因爲它不符合要求。容器的值類型必須是可複製和可複製分配的。

+0

其實這是行不通的。但我應該真的指出我的隊列不是std ::隊列,它實際上是一個併發隊列。看到我修改的問題。 – Matt 2010-11-01 01:25:47

+0

這是問題所在。我現在添加了一個拷貝構造函數和賦值運算符,並使其工作。謝謝。 – Matt 2010-11-01 03:22:01

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

這不會編譯,它是'InputQueue'矢量,而不是'InputQueue *'。另外,'std :: vector'有一個構造函數可以爲你循環,見我的答案。 – 2010-11-01 00:05:16

相關問題