Spottsworth
Spottsworth

Reputation: 415

C++: Vector of vectors

I need to create a vector of vectors (Vector of 3 vectors to be precise). Each constituent vector is of different datatype (String, double, user-defined datatype). Is this possible in C++? If not, is there any other elegant way of realizing my requirement?

Upvotes: 6

Views: 619

Answers (4)

Etienne de Martel
Etienne de Martel

Reputation: 36995

If you know there's three of them, and you know their types, why not just write a class?

class Data
{
    std::vector<std::string> _strings;
    std::vector<double> _doubles;
    std::vector<UserDefined> _userDefined;
public:
    // ...
};

This would also give some strong semantics (a vector of unrelated stuff seems weird in my opinion).

Upvotes: 14

xDD
xDD

Reputation: 3453

Is this what you mean?

vector<tuple<vector<string>, vector<double>, vector<T> > >

Upvotes: 0

seth
seth

Reputation: 1389

A struct or a class is in my opnion the best way to go, and is the most elegant solution.

Upvotes: 1

Puppy
Puppy

Reputation: 147036

template<typename T> struct inner_vectors {
    std::vector<double> double_vector;
    std::vector<std::string> string_vector;
    std::vector<T> user_vector;
};

std::vector<inner_vectors<some_type>> vector_of_vectors;

Upvotes: 3

Related Questions