What is difference between vector push_back insert and emplace function in cpp? | SoloLearn: Learn to code for FREE!

0

What is difference between vector push_back insert and emplace function in cpp?

6/3/2020 4:32:53 PM

Rushikesh Kate

3 Answers

New Answer

+3

The primary difference is that insert takes an object whose type is the same as the container type and copies that argument into the container. emplace takes a more or less arbitrary argument list and constructs an object in the container from those arguments.

+2

AMOGHA. A. K. Hi Pete Becker! :-) https://stackoverflow.com/a/15659336

0

suppose you have a vector of pairs: vector<pair<int, int>> v you want to insert something. v.push_back({a, b}); v.emplace_back(a, b); will yield the same results. emplace_back saves you from copying the object, so you just need to pass the arguments.