简体   繁体   中英

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.

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM