c++ - 具有 vector vector 的多种数据类型

标签 c++ arrays vector types

我正在尝试找到一种方法来创建具有三种不同类型的 3 维 vector ,其结构如下:

Vector[long][int][double];

我找到了很多示例来展示如何创建具有单一数据类型的 3d vector ,例如:

std::vector<vector<vector<int> > >;

但我现在可以找到或弄清楚如何将多种数据类型分配给 vector 。

最佳答案

如果你想同时使用所有三种类型,你应该使用一个结构。

struct Vector3d{
  long x;
  int y;
  double z;
};
//... or a union, if each entry only contains one type.
union NumberContainer
{
  long x;
  int y;
  double z;
};
std::vector<Vector3d> vector1;//Vector of three types
std::vector<NumberContainer> vector2;//Vector that can contain one of three types per entry
vector1[0].x=1;
vector1[0].y=2;
vector1[0].z=3;
//vector1 contains... x=1, y=2,z= 3
vector2[0].x=1;
vector2[0].y=2;
vector2[0].z=3;
//vector2 contains x=undefined, y=undefined, z=3

关于c++ - 具有 vector vector 的多种数据类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12063925/

相关文章:

c++ - 递归下降解析和语法树

c++ - 我怎样才能在内存中恰好有 2 位?

C++:这段代码有什么作用? foo([](){bar();});

c# - 有没有办法从 C++ 程序编译和执行 C# 代码?

JavaScript 拆分函数 - 在括号和逗号后拆分

arrays - 在 n 项数组中查找 k 最小数字的算法

c++ - XCode、命名空间、C++ 和代码可移植性

java - 将 double 组转换为 double ArrayList

c++ - 共线点

android - AnimatedVectorDrawable 可绘制路径的空路径