std::flat_set::replace(3)

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

✨ std::flat_set::replace(3) ✨

Okay bestie, welcome to the std::flat_set::replace(3) manual page from the C++ Standard Libary β€” yes, that manual name is folded in right up top, no header-line copy-paste happening here. This is the glow-up for replacing that underlying container. πŸ’…

🌸 NAME 🌸

std::flat_set::replace β€” the NAME line is literally std::flat_set::replace - std::flat_set::replace. It's giving "I contain myself," and honestly? Iconic. This page explains the member that replaces the underlying container for a std::flat_set.

πŸ¦‹ Synopsis πŸ¦‹

Here's the signature, no cap:

void replace( container_type&& cont );

Since C++23. ✨

So what does it do? It replaces the underlying container, c, and it's equivalent to:

c = std::move(cont);
Heads up, bestie: the elements of cont must be sorted with respect to compare, and cont must not contain equal elements. Otherwise, the behavior is undefined. This is the "do not skip the vibe check" part. 😭

πŸŽ€ Parameters πŸŽ€

  • cont β€” a sorted container of type KeyContainer, the contents of which will be moved into *this. 🫢

⭐ Return value ⭐

(none) β€” nothing comes back, bestie. It's giving "I did the move and I'm not returning your texts." πŸ’€

πŸ’œ Complexity πŸ’œ

Equals the complexity of std::move applied to the adapted container. No secret extra drama, no hidden plot twist β€” that's the whole complexity story. ✨

🌈 Example 🌈

// Run this code
#include <algorithm>
#include <cassert>
#include <flat_set>
#include <print>
#include <vector>

int main()
{
    std::vector<int> keys{1, 2, 3};
    assert(std::ranges::is_sorted(keys));

    std::flat_set<int> set;
    assert(set.empty());

    set.replace(keys);
    assert(set.size() == 3);
    assert(keys.empty());

    std::println("{}", set); // set.keys()
}

πŸ’– Output πŸ’–

[1, 2, 3]

Ate. Periodt. πŸ“

πŸ‘‘ See also πŸ‘‘

  • extract β€” extracts the underlying container (public member function). Go off, extract. πŸ’…

Source/version/date sign-off: this iconic little page comes from http://cppreference.com, dated 2024.06.10, and it is the std::flat_set::replace(3) manual page. Mwah πŸ’‹βœ¨

πŸ“¦ where this page comes from