I'm trying to find a way in C++ to have a dynamic size array (vector) in which I can store arrays of strings. This concept shall be used to store data with different labels, e.g.:
{"Car", "0", "0", "400"},
{"Person", "0", "1", "320"},
{"Bicycle", "1", "0", "300"},
...
After a bit internet search, I came this far:
struct Stringvec {std::string element[4];};
std::vector<Stringvec> list;
list.push_back({"Car", "0", "0", "400"});
But I cannot figure out how to access the individual elements of the arrays I store in the vector, e.g. "Car". This won't work:
std::string label = list[0][0]; //Gives error:
error: no match for 'operator[]' (operand types are '__gnu_cxx::__alloc_traits<std::allocator<Stringvec> >::value_type {aka Stringvec}' and 'int')
Anyone knows how to get around this or an even better way to store string arrays in dynamic size arrays?
You can try using std::array
within std::vector
vector<array<string, 4>> list = {
{"Car", "0", "0", "400"},
{"Bicycle", "1", "2", "500"}
};
Then you can access the elements you want to access just like you want:
string str = list[0][1];