how to pushback on a vector of vectors?

c++, vector

Solution

The code is right, but your vector has zero elements in it so you cannot access `big[i]`.

Set the vector size before the loop, either in the constructor or like this:

big.resize(ruleNum);

Alternatively you can push an empty vector in each loop step:

big.push_back( vector<string>() );

You don't need the parentheses around `big[i]` either.

Problem

I am taking 20 lines of input. I want to separate the contents of each line by a space and put it into a vector of vectors. How do I make a vector of vectors? I am having have struggles pushing it back... My input file: ``` Mary had a little lamb lalala up the hill the sun is up ``` The vector should look like something like this. ``` ROW 0: {"Mary","had", "a","little","lamb"} ROW 1: {"lalala","up","the","hill"} ``` This is my code.... ``` string line; vector <vector<string> > big; string buf; for (int i = 0; i < 20; i++){ getline(cin, line); stringstream ss(line); while (ss >> buf){ (big[i]).push_back(buf); } } ```

Original source