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.
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 used to construct a new object of the mapped type for the inserted element.
Arguments forwarded to construct the new element (of type
pair<const key_type, mapped_type>
).
This can be one of:
- Two arguments: one for the
key, the other for the
mapped value.
- A single argument of a
pair
type with a value for the
key as first member, and a value for the
mapped value as second.
-
piecewise_construct as first argument, and two additional arguments with
tuples to be forwarded as arguments for the
key value and for the
mapped value respectivelly.
See
pair::pair for more info.
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.