在vector中存储不同维数的数组

Storing arrays of different dimension in vector

本文关键字:数组 vector 存储      更新时间:2023-10-16

我想在一个向量中存储两个浮点数数组,如下所示

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 []’

请建议如何做到这一点!

两个不同维度的数组有不同的类型。因此它们不能存储在同一个向量中。你可以把它们变成一个矢量并存储为

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支持,可以这样初始化vector

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