2013-04-24 111 views
1

不同尺寸的陣列花車的兩個數組存儲在一個向量像這樣存儲在矢量

float b_pt[16] = {-65.,-55.,-45.,-40.,-35.,-30.,-25.,-20.,20.,25.,30.,35.,40.,45.,55.,65.}; 
    float b_eta[25] = 
    {-4.5,-4.25,-4.,-3.75,-3.5,-3.25,-3.,-2.75,-2.5,-2.25,-2.,-1.,0.,1.,2.,2.25,2.5,2.75,3.,3.25,3.5,3.75,4.,4.25,4.5}; 

vector<float[]> bins; 
bins.push_back(b_pt); 
bins.push_back(b_eta); 

但得到以下錯誤:

g++ -g -Wall -W -Wconversion -Wshadow -Wcast-qual -Wwrite-strings -pthread -m64 -I/opt/local/root-v5-34-00/include -L/opt/local/root-v5-34-00/lib -lGui -lCore -lCint -lRIO -lNet -lHist -lGraf -lGraf3d -lGpad -lTree -lRint -lPostscript -lMatrix -lPhysics -lMathCore -lThread -lpthread -Wl,-rpath,/opt/local/root-v5-34-00/lib -lm -ldl -MMD -MP -c -o test.o test.cpp 
test.cpp: In function ‘int main()’: 
test.cpp:31: error: no matching function for call to ‘std::vector<float [], std::allocator<float []> >::push_back(float [16])’ 
/usr/include/c++/4.2.1/bits/stl_vector.h:600: note: candidates are: void std::vector<_Tp, _Alloc>::push_back(const _Tp&) [with _Tp = float [], _Alloc = std::allocator<float []>] 
test.cpp:32: error: no matching function for call to ‘std::vector<float [], std::allocator<float []> >::push_back(float [25])’ 
/usr/include/c++/4.2.1/bits/stl_vector.h:600: note: candidates are: void std::vector<_Tp, _Alloc>::push_back(const _Tp&) [with _Tp = float [], _Alloc = std::allocator<float []>] 
/usr/include/c++/4.2.1/bits/stl_vector.h: In destructor ‘std::_Vector_base<_Tp, _Alloc>::~_Vector_base() [with _Tp = float [], _Alloc = std::allocator<float []>]’: 
/usr/include/c++/4.2.1/bits/stl_vector.h:202: instantiated from ‘std::vector<_Tp, _Alloc>::vector(const _Alloc&) [with _Tp = float [], _Alloc = std::allocator<float []>]’ 
test.cpp:30: instantiated from here 
/usr/include/c++/4.2.1/bits/stl_vector.h:123: error: invalid use of array with unspecified bounds 
/usr/include/c++/4.2.1/bits/stl_construct.h: In function ‘void std::__destroy_aux(_ForwardIterator, _ForwardIterator, std::__false_type) [with _ForwardIterator = float (*)[]]’: 
/usr/include/c++/4.2.1/bits/stl_construct.h:155: instantiated from ‘void std::_Destroy(_ForwardIterator, _ForwardIterator) [with _ForwardIterator = float (*)[]]’ 
/usr/include/c++/4.2.1/bits/stl_construct.h:182: instantiated from ‘void std::_Destroy(_ForwardIterator, _ForwardIterator, std::allocator<_T2>) [with _ForwardIterator = float (*)[], _Tp = float []]’ 
/usr/include/c++/4.2.1/bits/stl_vector.h:271: instantiated from ‘std::vector<_Tp, _Alloc>::~vector() [with _Tp = float [], _Alloc = std::allocator<float []>]’ 
test.cpp:30: instantiated from here 
/usr/include/c++/4.2.1/bits/stl_construct.h:121: error: cannot increment a pointer to incomplete type ‘float []’ 

請就如何建議做這個!

回答

3

不同尺寸的兩個陣列有不同的尺寸類型。因此它們不能被存儲在相同的向量中。 可以代替使他們成爲一個向量並存儲爲

vector<float> b_pt = {-65.,-55.,-45.,-40.,-35.,-30.,-25.,-20.,20.,25.,30.,35.,40.,45.,55.,65.}; 
vector<float> b_eta = {-4.5,-4.25,-4.,-3.75,-3.5,-3.25,-3.,-2.75,-2.5,-2.25,-2.,-1.,0.,1.,2.,2.25,2.5,2.75,3.,3.25,3.5,3.75,4.,4.25,4.5}; 
vector<vector<float>> bins; 
bins.push_back(b_pt); 
bins.push_back(b_eta); 

或使用動態分配和存儲指針浮陣列和存儲爲

float* b_pt = new float[16] {-65.,-55.,-45.,-40.,-35.,-30.,-25.,-20.,20.,25.,30.,35.,40.,45.,55.,65.}; 
vector<float*> bins; 
bins.push_back(b_pt); 

// later for each i 
delete [] bins[i]; 

我會因爲這樣的話,你不要第一個選項去不必擔心泄漏的記憶。


如果你沒有C++ 1的支持,你可以用這種方式初始化向量。

float b_pt_data[16] = {-65.,-55.,-45.,-40.,-35.,-30.,-25.,-20.,20.,25.,30.,35.,40.,45.,55.,65.}; 
vector<float> b_pt(b_pt_data, b_pt_data + 16); 
vector<vector<float> > bins; 
bins.push_back(b_pt); 
+0

謝謝,但的代碼在第二示例中的第一行不編譯:錯誤:預期「」或‘;’前‘{’令牌 – user2099143 2013-04-24 11:10:13

+0

@ user2099143哦對不起只在C可用++ 11。我想如果你沒有C++ 11的支持,你將不得不逐個填充數組。 – stardust 2013-04-24 11:15:47

+0

這是正確的方法,但不能以這種方式初始化向量。請參閱http://stackoverflow.com/questions/2236197/c-easiest-way-to-initialize-an-stl-vector-with-hardcoded-elements – Roddy 2013-04-24 11:15:51