在vector中存储不同维数的数组
Storing arrays of different dimension in vector
我想在一个向量中存储两个浮点数数组,如下所示
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);
相关文章:
- 在c++中用vector填充一个简单的动态数组
- std::vector的包装器,使数组的结构看起来像结构的数组
- 将 int 数组转换为 std::vector<int*>
- 为什么std::vector比数组慢
- C++ 无法将 std::vector<BYTE> 转换为字符串或字符数组
- 如何将数组的内容附加到 std::vector?
- 在 C++ 中使用 Vector 在动态数组中插入值,循环
- 为什么优化大型 std::vector 数组会导致 SegFault?
- SFML访问std::vector内的数组
- 如何在C++中将 std::vector 移动到原始数组中
- 如何使 std::vector<T> 使用现有的 T* 数组而不复制这些值?
- 将 unique_ptr<std::vector> 与 c 样式数组结合使用
- 将 2D 数组插入 std::vector 时"cannot convert from 'const GLfloat [12]' to '_Objty'"错误消息
- 查找索引数据结构,例如`sTD :: vector`(非数组)
- 如何在多个文件中声明全局 std::vector 2d 数组?C++
- 删除[]是否可以与通用数组正常使用?如果是这样,为什么使用std :: vector ::擦除它会导致释放内存的错误
- 使用 ::std::vector 创建数组
- 为什么"new vector<int>[5]"不创建向量数组?
- 如何将std ::数组转换为std :: vector
- Bool Vector 2D数组在IF条件下使用