multimap::emplace
Inserts an element constructed in place (no copy or move operations are performed).
template<class... Args>
iterator emplace(
Args&&... args);
Parameters
Parameter |
Description |
args |
The arguments forwarded to construct an element to be inserted into the multimap. |
Return Value
An iterator to the newly inserted element.
Remarks
No references to container elements are invalidated by this function, but it may invalidate all iterators to the container.
If an exception is thrown during the insertion, the container is left unaltered and the exception is rethrown.
The value_type of an element is a pair, so that the value of an element will be an ordered pair with the first component equal to the key value and the second component equal to the data value of the element.
Example
// multimap_emplace.cpp
// compile with: /EHsc
#include <map>
#include <string>
#include <iostream>
using namespace std;
template <typename M> void print(const M& m) {
cout << m.size() << " elements: " << endl;
for (const auto& p : m) {
cout << "(" << p.first << "," << p.second << ") ";
}
cout << endl;
}
int main()
{
multimap<string, string> m1;
m1.emplace("Anna", "Accounting");
m1.emplace("Bob", "Accounting");
m1.emplace("Carmine", "Engineering");
cout << "multimap modified, now contains ";
print(m1);
cout << endl;
m1.emplace("Bob", "Engineering");
cout << "multimap modified, now contains ";
print(m1);
cout << endl;
}
Output
multimap modified, now contains 3 elements:
(Anna,Accounting) (Bob,Accounting) (Carmine,Engineering)
multimap modified, now contains 4 elements:
(Anna,Accounting) (Bob,Accounting) (Bob,Engineering) (Carmine,Engineering)
Requirements
Header: <map>
Namespace: std