使用C++和STL,是否有人知道如何將整數數組存儲爲STL列表或向量中的節點?我需要存儲數量未知的數字對,並且來自其他語言,我首先想到的是使用某種類型的列表或向量數據結構...但我遇到了一些麻煩。我100%肯定自己正在犯一個明顯的初學者的C++錯誤,而那些真正瞭解這門語言的人會看看我想要做的事情,並且能夠讓我直觀。如何將數組存儲在STL列表中?
所以,這是我試過的。聲明一個列表,像這樣的工作:
stl::list<int[2]> my_list;
,然後我可以很容易地使兩個元素的數組,像這樣:
int foo[2] = {1,2};
這編譯和運行就好了。然而,當我嘗試添加foo
我的名單,像這樣:
my_list.push_back(foo);
我得到一個整體粗糙集編譯器錯誤,其中沒有我真的能理解(我的C++的 - 福幾乎是不存在的):
/usr/include/c++/4.0.0/ext/new_allocator.h: In member function ‘void __gnu_cxx::new_allocator<_Tp>::construct(_Tp*, const _Tp&) [with _Tp = int [2]]’:
/usr/include/c++/4.0.0/bits/stl_list.h:440: instantiated from ‘std::_List_node<_Tp>* std::list<_Tp, _Alloc>::_M_create_node(const _Tp&) [with _Tp = int [2], _Alloc = std::allocator<int [2]>]’
/usr/include/c++/4.0.0/bits/stl_list.h:1151: instantiated from ‘void std::list<_Tp, _Alloc>::_M_insert(std::_List_iterator<_Tp>, const _Tp&) [with _Tp = int [2], _Alloc = std::allocator<int [2]>]’
/usr/include/c++/4.0.0/bits/stl_list.h:773: instantiated from ‘void std::list<_Tp, _Alloc>::push_back(const _Tp&) [with _Tp = int [2], _Alloc = std::allocator<int [2]>]’
test.cpp:5: instantiated from here
/usr/include/c++/4.0.0/ext/new_allocator.h:104: error: ISO C++ forbids initialization in array new
那麼,有人有什麼想法,我在做什麼錯在這裏?任何指針(沒有雙關語意圖)將是最有幫助的。是不是可以存儲數組在一個std :: list?我應該使用一個結構?我只是在某處丟失了*
或&
?
標準中的定義不提供第二種類型的默認類型,因此您必須明確提供這兩種類型。 – 2009-05-05 22:16:57