public member function
<map>
template <class... Args>
iterator emplace (Args&&... args);
Construct and insert element
Inserts a new element in the multimap. This new element is constructed in place using args as the arguments for the construction of a value_type (which is an object of a pair type).
This effectively increases the container size by one.
Internally, multimap containers keep all their elements sorted by key following the criterion specified by its comparison object. The element is always inserted in its respective position following this ordering.
The element is constructed in-place by calling allocator_traits::construct with args forwarded.
A similar member function exists, insert, which either copies or moves existing objects into the container.
There are no guarantees on the relative order of elements with equivalent keys.
The relative ordering of elements with equivalent keys is preserved, and newly inserted elements follow those with equivalent keys already in the container.
Parameters
- args
- Arguments forwarded to construct the new element (of type pair<const key_type, mapped_type>).
Return value
An iterator to the newly inserted element.
Member type iterator is a bidirectional iterator type that points to an element.
Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
|
// multimap::emplace
#include <iostream>
#include <string>
#include <map>
int main ()
{
std::multimap<std::string,float> mymultimap;
mymultimap.emplace("apple",1.50);
mymultimap.emplace("coffee",2.10);
mymultimap.emplace("apple",1.40);
std::cout << "mymultimap contains:";
for (auto& x: mymultimap)
std::cout << " [" << x.first << ':' << x.second << ']';
std::cout << '\n';
return 0;
}
|
Output:
mymultimap contains: [apple:1.5] [apple:1.4] [coffee:2.1]
|
Complexity
Logarithmic in the container size.
Iterator validity
No changes.
Data races
The container is modified.
Concurrently accessing existing elements is safe, although iterating ranges in the container is not.
Exception safety
Strong guarantee: if an exception is thrown, there are no changes in the container.
If allocator_traits::construct is not supported with the appropriate arguments, it causes undefined behavior.