LucasS
LucasS

Reputation: 729

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:

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:

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

But I can now find or figure out how to assign multiple data types to the vector.

Upvotes: 2

Views: 11215

Answers (3)

Brian Kocoloski
Brian Kocoloski

Reputation: 61

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

Upvotes: 2

Skyler Saleh
Skyler Saleh

Reputation: 3991

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

Upvotes: 4

David
David

Reputation: 28168

Conceptually Vector[long][int][double] doesn't make any sense. 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. Make a vector of a type that contains your 3 values: your own struct probably.

Upvotes: 3

Related Questions