C++: Vector of vectors

折月煮酒 提交于 2019-12-23 09:29:45

问题


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?


回答1:


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




回答2:


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:


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




回答4:


Is this what you mean?

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


来源:https://stackoverflow.com/questions/6190414/c-vector-of-vectors

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!