简体   繁体   English

向量与向量的多种数据类型

[英]Multiple Data Types with Vectors of Vectors

I am trying to find a way to create a 3 dimensional vector with three different types, such that it is structured as: 我试图找到一种方法来创建具有三种不同类型的3维矢量,使其结构如下:

Vector[long][int][double];

I have found plenty of examples that show how to create a 3d vector with a single data type, such as: 我发现了很多示例,这些示例展示了如何创建具有单一数据类型的3d矢量,例如:

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

But I can now find or figure out how to assign multiple data types to the vector. 但是我现在可以找到或弄清楚如何为向量分配多种数据类型。

You should use a struct if you wish to use all three types at the same time. 如果希望同时使用所有三种类型,则应使用结构。

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

Conceptually Vector[long][int][double] doesn't make any sense. 从概念上讲, Vector[long][int][double]没有任何意义。 You can have a vector of vectors of vectors of something . 您可以拥有某物的向量的向量。 There's only 1 type of something in the end. 最终只有一种类型的东西

Take a step out of dimensionality. 迈出维度。 If you're just trying to contain 3 values per element in a vector you can do that a number of ways. 如果您只想在向量中每个元素包含3个值,则可以采用多种方法。 Make a vector of a type that contains your 3 values: your own struct probably. 创建一个包含您的3个值的类型的向量:可能是您自己的结构。

At the end of the day, your data structure has to hold something , and that something can only be of one type. 归根结底,您的数据结构必须容纳某种东西 ,而某种东西只能是一种类型。 Now, if you want to store multiple data types in each location of your vector, your "something" can itself be a struct of multiple different types. 现在,如果要在向量的每个位置存储多种数据类型,则“内容”本身可以是多种不同类型的结构。

It would help if you provided a little more context 如果您提供更多背景信息将有所帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM