std::flat_m...map::emplace(3)
π NAME π
std::flat_multimap::emplace β std::flat_multimap::emplace
Okay so this page is from the C++ Standard Libary manual, page 3, and it's literally all about std::flat_multimap::emplace. Bestie, we're inserting a brand-new element into the container, constructed in-place with the given args. In-place construction is the main character here. πβ¨
β¨ Synopsis β¨
template< class... Args > (since C++23)
iterator emplace( Args&&... args );
Inserts a new element into the container constructed in-place with the given args. π οΈ
It initializes an object t of type std::pair<key_type, mapped_type> with std::forward<Args>(args)...; if the map already contains an element whose key is equivalent to t.first, *this is unchanged. Otherwise, it's equivalent to:
auto key_it = ranges::upper_bound(c.keys, t.first, compare);
auto value_it = c.values.begin() + std::distance(c.keys.begin(), key_it);
c.keys.insert(key_it, std::move(t.first));
c.values.insert(value_it, std::move(t.second));
This overload participates in overload resolution only if std::is_constructible_v<std::pair<key_type, mapped_type>, Args...> is true. No fake overloads, no maybes. π
Careful use of emplace allows the new element to be constructed while avoiding unnecessary copy or move operations. That's the whole slay of it. β¨
Information on iterator invalidation is copied from here. π
πΈ Parameters πΈ
argsβ arguments to forward to the constructor of the element.
π«Ά Return value π«Ά
An iterator to the inserted element. That's it, that's the tea. π
π¨ Exceptions π¨
If an exception is thrown for any reason, this function has no effect β that's the strong exception safety guarantee, periodt. π
π₯ Complexity π₯
Linear in the size of the container. π
π Example π
// Run this code
#include <iostream>
#include <string>
#include <utility>
#include <flat_map>
int main()
{
std::flat_multimap<std::string, std::string> m;
// uses pair's move constructor
m.emplace(std::make_pair(std::string("a"), std::string("a")));
// uses pair's converting move constructor
m.emplace(std::make_pair("b", "abcd"));
// uses pair's template constructor
m.emplace("d", "ddd");
// emplace with duplicate key
m.emplace("d", "DDD");
// uses pair's piecewise constructor
m.emplace(std::piecewise_construct,
std::forward_as_tuple("c"),
std::forward_as_tuple(10, 'c'));
for (const auto& p : m)
std::cout << p.first << " => " << p.second << '\n';
}
Output:
a => a
b => abcd
c => cccccccccc
d => ddd
d => DDD
π See also π
emplace_hintβ constructs elements in-place using a hint (public member function).try_emplaceβ inserts in-place if the key does not exist, does nothing if the key exists (public member function).insertβ inserts elements (public member function).
That's the whole page, bestie β from the C++ Standard Libary crew at http://cppreference.com, dated 2024.06.10, and filed under std::flat_m...map::emplace(3). Go forth and emplace responsibly. ππ¦
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms