C ++: vector of vectors

I need to create a vector of vectors (more precisely, a vector of 3 vectors). Each component vector has a different data type (String, double, user-defined data type). Is this possible in C ++? If not, is there another elegant way to fulfill my requirement?

+6
source share
4 answers

If you know 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: // ... }; 

It will also give some strong semantics (the vector of unrelated material seems strange in my opinion).

+14
source
 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; 
+3
source

The structure or class in my example is the best way to go, and this is the most elegant solution.

+1
source

Is that what you mean?

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

Source: https://habr.com/ru/post/889465/


All Articles