Why can't I add an item to vector :
std::vector < std::vector <std::string> > my_list;
my_list[0][0] = "this is text";
std::cout << "text: " << my_list[0][0] << std::endl;
What am I doing wrong?
Why can't I add an item to vector :
std::vector < std::vector <std::string> > my_list;
my_list[0][0] = "this is text";
std::cout << "text: " << my_list[0][0] << std::endl;
What am I doing wrong?
Unlike some containers (such as std::map
), a std::vector
does not grow on demand.
So before you index an element you need to make sure the std::vector
has the appropriate size. You can do that by passing a size on construction.
In your case you could use the flashy syntax
std::vector < std::vector <std::string> > my_list{{"this is a test"}};
to get things going.