confusion123
confusion123

Reputation: 15

Adding elements to a 2D vector (Vector of vectors) C++

So lets say i have the:

vector<vector<int> > temp;

so it'd just be an empty 2d vector and pretend it's filled with 0s so it'd look like:

0  0
0  0
0  0
0  0

and let's say I want to add something to that vector, like if I had:

int x = 3, y = 4;

I want to push these onto the vector so that it looks something like:

0  0
0  0
0  0
3  4

and I've tried: temp.push_back(x,y); but this isn't the correct syntax.

Upvotes: 0

Views: 171

Answers (1)

PaulMcKenzie
PaulMcKenzie

Reputation: 35440

Since temp has inside a vector<int>, the correct syntax would be:

temp.push_back({x,y});

Using the brace-initializer list, this creates another vector consisting of two elements, x and y, and pushes that back onto temp.

Upvotes: 2

Related Questions