C++,创建向量的向量
c++, creating vector of vectors?
问题是关于创建vector
vector
的最佳方法是什么。我有几个vector<double> coordinates;
,我想让它们发挥作用。vector<vector<double> >
,我应该如何组合它们?还有比这更优雅的方式吗?
这听起来像是一个合理的方法。 如果您担心可读性,请使用typedef
。
但是,如果所有向量都是相同的长度(例如,您确实在尝试创建 2D 数组(,请考虑使用 boost::multi_array
。
就像你说的看起来不错:
void foo(vector<vector<double> > &);
int main()
{
vector<double> coordinates1, coordinates2, coordinates3;
//...
vector<vector<double> > CoordinateVectors;
CoordinateVectors.push_back(coordinates1);
CoordinateVectors.push_back(coordinates2);
CoordinateVectors.push_back(coordinates3);
foo(CoordinateVectors);
return 0;
}
也许是这样的:
typedef vector<double> coords_vec_type;
typedef vector<coords_vec_type> coords_vec2_type;
void foo(coords_vec2_type& param) {
}
或者使用指针来避免复制,如果源向量已经在某个地方:
typedef vector<coords_vec_type*> coords_vec2_ptr_type;
另一种选择是将向量放入数组并将其传递给函数,例如:
void foo(std::vector<double> **vecs, int numVecs)
{
...
}
int main()
{
std::vector<double> coordinates1, coordinates2, coordinates3;
//...
std::vector<double>* CoordinateVectors[3];
CoordinateVectors[0] = &coordinates1;
CoordinateVectors[1] = &coordinates2;
CoordinateVectors[2] = &coordinates3;
foo(CoordinateVectors, 3);
return 0;
}
或:
void foo(std::vector<double> *vecs, int numVecs)
{
...
}
int main()
{
std::vector<double> coordinates[3];
//...
foo(coordinates, 3);
return 0;
}
相关文章:
- 从具有自定义排序的向量创建集合
- 我正在寻找一种优雅的方式来从元组向量创建tuple_element向量
- 从类指针的向量创建成员变量的向量
- 从 c++ 中的字符串向量创建一个元组
- 如何在 c++ 中从两个向量创建 JSON 对象?
- 如何在不迭代的情况下从string_view向量创建字符串向量?
- 通过移动从两个向量创建元组向量
- 为结构的向量创建C 矢量迭代器
- C 矢量元素擦除与新向量创建
- 使用向量的向量创建一个尺寸
- C MultiSet从排序的向量创建
- 从向量<T>创建一组字符串
- 使用C 中的向量创建整数集类别
- 从两个原始向量创建一个“特征::矩阵”
- 用向量创建筛子
- 从Vectores的向量创建一个板
- C++内存管理:从函数中的向量创建一个新数组
- 正在使用类中的向量创建类
- 为向量创建临时变量
- 如何在c++中使用模板图类中的向量创建邻接矩阵