std::flat_map::replace(3)

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

πŸ’… std::flat_map::replace(3) πŸ’…

okay so bestie, welcome to the C++ Standard Libary page for std::flat_map::replace β€” yes the manual name is literally "C++ Standard Libary" (not a typo, that's just how it's spelled in the header, iykyk). this one's a member function that lets you swap out the whole underlying container situation in one move. no cap, it's giving main character energy for flat_map. ✨


πŸ“› NAME πŸ“›

std::flat_map::replace β€” literally just std::flat_map::replace. that's the name. periodt. πŸ’…


✨ Synopsis ✨

the squad's signature, since C++23, fr fr:

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

so what does it do? it replaces the underlying containers c. equivalent to this little move-move situation:

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

but like, you gotta meet these conditions or it's a whole mess:

  • the expression key_cont.size() == mapped_cont.size() is true (sizes gotta match, no cap)
  • the elements of key_cont are sorted with respect to compare (sorted, bestie, sorted)
  • key_cont does not contain equal elements (no duplicates, we don't do that here)
⚠️ Otherwise, the behavior is undefined. like, undefined undefined. don't play with this. πŸ’€

πŸŽ€ Parameters πŸŽ€

  • keys_cont β€” a sorted keys container of type KeyContainer, the contents of which will be moved into *this. it's the key VIP, literally. πŸ”‘
  • mapped_cont β€” a container of mapped values of type MappedContainer, the contents of which will be moved into *this. the plus-one to the key container, they're a package deal. 🫢

🌸 Return value 🌸

(none). it's void, bestie. it does the thing and dips. no return, no drama. πŸ™ˆ


πŸ¦‹ Complexity πŸ¦‹

equals to complexity of std::move applied to adapted containers. that's it. that's the tea. β˜•


πŸ”₯ Example πŸ”₯

run this code, it's iconic:

#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_map<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}

and that's on periodt. the map ate and left no crumbs. πŸ’…πŸ”₯


πŸ‘‘ See also πŸ‘‘

  • extract β€” extracts the underlying containers (public member function). it's the sibling function, also a vibe. go check it out. 🌈

okay bestie, that's the whole page. we got the header info ("C++ Standard Libary" manual, the source is http://cppreference.com, version 2024.06.10, date 2024.06.10, and the page is std::flat_map::replace(3)). now go slay your flat_map containers. mwah. πŸ’–βœ¨πŸŽ€

πŸ“¦ where this page comes from