I would like to construct a vector of vectors. This question has already been posted many time on SO but I did not find a satisfying answer. That because:
Basically I would like to be able to do something like that in pseudo code later
types = ["char", "int", "double", "int"]
container<vector> x
foreach (type in types)
{
if (type == "char")
x.push_back(vector<char>)
else if (type == "int")
x.push_back(vector<int>)
else
x.push_back(vector<double>)
}
and then I would like to be able to do for example
x[0].push_back("a")
x[1].push_back(1)
x[2].push_back(3.1416)
I think boost::any may help me but I'm not familiar with boost yet.
The point is, even if it sound weird, it is really what I want to do. I don't want a vector of structures, I really want a container (no matter which one) containing std::vector of different types. The reason is because I'm reading binary files. The header of the file states the number of data and their types but it can change from a file to another. Thus it cannot be known at compilation time.
Perhaps you could use a vector of variants?
using ints = std::vector<int>;
using chars = std::vector<char>;
using doubles = std::vector<double>;
using mixed_data_t = std::vector<std::variant<chars, ints, doubles>>;
If each file has the same type of data you can find out what it is at run time and push back into the appropriate variant-vector.
If the files have mixed data then you could use
std::vector<std::vector<std::variant<char, int, double>>>;
but you will have to check on each insertion.
I'm fairly confident that boost::any is not the solution you are looking for as type information will be lost after each insertion.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With