std::flat_m...map::emplace(3)

πŸ“¦ version 2024.07.05, in nixpkgs unstable, opensuse-leap 16.0

πŸŽ€ 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