我想在这样的矢量中存储两个浮点数组
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 []’
请告知如何做到这一点!
答案 0 :(得分: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数组并存储为
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);