I didn't find anyone answering this, is there any difference between the following :
v.push_back({x, y});
and :
v.push_back(make_pair(x, y));
Assuming that v was declared this way :
vector<pair<int,int> > v;
I didn't find anyone answering this, is there any difference between the following :
v.push_back({x, y});
and :
v.push_back(make_pair(x, y));
Assuming that v was declared this way :
vector<pair<int,int> > v;
{x, y}
inv.push_back({x, y})
is aggregate initialization (since C++11) ofv
'svalue_type
, whereasstd::make_pair
is a function creating andstd::pair
with types deduced from its arguments.One advantage of
push_back({x, y})
overemplace_back(x, y)
is that you could keep small structures simple (without constructors) like this:Example.
I think you might have accepted that answer a little too quickly. The commonly accepted way to do this is like this:
And if you look at Godbolt, you can see that this inlines everything (which may or may not be what you want):
https://godbolt.org/z/aCl02d
Run it at Wandbox:
https://wandbox.org/permlink/uo3OqlS2X4s5YpB6
Code:
Output:
Of course, everything runs faster if you reserve the appropriate number of elements in the vector up front. Maybe that's what the people posing this question are really wanting you to say.
I tried this in an online compiler, and as far as I can see the optimized assembly for make_pair is identical to {} syntax.
https://godbolt.org/z/P7Ugkt