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

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

okay so this is the std::flat_m...map::replace(3) page β€” folded in from the C++ Standard Libary manual β€” and bestie, we are about to make replacing your underlying containers feel like a main-character move. πŸ’…βœ¨


🌸 NAME 🌸

std::flat_multimap::replace β€” that's the name, and the name is std::flat_multimap::replace. No plot twist, just a member function ready to swap your underlying containers. πŸ’…


✨ Synopsis ✨

Here's the whole vibe β€” this one is since C++23:

void replace( key_container_type&& key_cont, mapped_container_type&& mapped_cont );

It replaces the underlying containers c. Equivalent to:

c.keys = std::move(key_cont);
c.values = std::move(mapped_cont);

The following conditions must be met:

  • The expression key_cont.size() == mapped_cont.size() is true,
  • The elements of key_cont are sorted with respect to compare.
🚨 Otherwise, the behavior is undefined. Not "maybe fine", not "we'll see" β€” undefined. Protect your peace and your program, bestie.

πŸŽ€ Parameters πŸŽ€

  • keys_cont β€” a sorted keys container of type KeyContainer; its contents will be moved into *this. 🫢
  • mapped_cont β€” a container of mapped values of type MappedContainer; its contents will be moved into *this. πŸ’–

πŸ¦‹ Return value πŸ¦‹

(none). That's it. No return, no drama. πŸŽ€


πŸ”₯ Complexity πŸ”₯

Equals to complexity of std::move applied to adapted containers. That's the whole sentence β€” we're not adding lore. πŸ’€


🌈 Example 🌈

// Run this code

#include <algorithm>
#include <cassert>
#include <flat_map>
#include <print>
#include <vector>

int main()
{
    std::vector<int> keys{1, 2, 3};
    assert(std::ranges::is_sorted(keys));
    std::vector<double> values{2.2, 3.3, 1.1};
    assert(keys.size() == values.size());

    std::flat_multimap<int, double> map;
    assert(map.empty());

    map.replace(keys, values);
    assert(map.size() == 3);
    assert(map.keys() == 3);
    assert(map.values() == 3);
    assert(keys.empty());
    assert(values.empty());

    std::println("{}", map);
}

πŸ“ Output πŸ“

{1: 2.2, 2: 3.3, 3: 1.1}

πŸ‘‘ See also πŸ‘‘

  • extract β€” extracts the underlying containers (public member function). πŸ”₯

cute sign-off: source/version is http://cppreference.com, date is 2024.06.10, page is std::flat_m...map::replace(3). mwah πŸ’‹πŸ«Ά

πŸ“¦ where this page comes from