gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-03 08:27:44 25 4
gpt4 key购买 nike

我正在尝试找到一种方法来创建具有三种不同类型的 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com