emplace vs insert in C++ STL
In C++, all containers (vector, stack, queue, set, map, etc) support both insert and emplace operations.
The advantage of emplace is, it does in-place insertion and avoids an unnecessary copy of object. For primitive data types, it does not matter which one we use. But for objects, use of emplace() is preferred for efficiency reasons.
it = ms.begin(); it != ms.end(); ++it)
<< (*it).first <<
<< (*it).second << endl;
Please refer Inserting elements in std::map (insert, emplace and operator ) for details.
Attention reader! Don’t stop learning now. Get hold of all the important C++ Foundation and STL concepts with the C++ Foundation and STL courses at a student-friendly price and become industry ready.