开发者

C++: Vector of vectors

开发者 https://www.devze.com 2023-03-09 06:48 出处:网络
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

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 reali开发者_JAVA百科zing my requirement?


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).


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;


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


Is this what you mean?

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

精彩评论

暂无评论...
验证码 换一张
取 消