2013-04-24 108 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