Dave
Dave

Reputation: 474

Adding an element to vector inside a vector in C++

Simply, I have a vector:

vector<vector<int>> myvector

For this example it would look like this:

myvector[
vector{1,2,3}
vector{1,2,3,4,5}
vector{1,2}
]

How would one go about adding a number such as 4 to the first vector inside myvector to make it look like below?:

myvector[
vector{1,2,3,4}
vector{1,2,3,4,5}
vector{1,2}
]

Upvotes: 0

Views: 55

Answers (2)

Farhad
Farhad

Reputation: 4181

you can access to them with indexes:

vectorOne[index].push_back(1);

More info:

http://www.cplusplus.com/reference/vector/vector/

http://www.cplusplus.com/forum/beginner/12409/

Upvotes: 0

Remy Lebeau
Remy Lebeau

Reputation: 595537

Like this:

myvector[0].push_back(4);

Upvotes: 3

Related Questions