std::unorde...ltimap::swap(3)

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

okay so this is the std::unorde...ltimap::swap(3) page from the C++ Standard Libary manual β€” title and manual name folded in right here, bestie πŸ’– (yes, the header is truncated, that’s the whole vibe).

🌸 NAME 🌸

std::unordered_multimap::swap β€” std::unordered_multimap::swap. Iconic, repetitive, we love her. 🫢

✨ Synopsis ✨

Two overload eras, bestie β€” pay attention to the (since C++11) / (until C++17) / (since C++17) labels:

void swap( unordered_multimap& other );

(since C++11) (until C++17)

void swap( unordered_multimap& other ) noexcept(/* see below */);

(since C++17)

Exchanges the contents of the container with those of other. Does not invoke any move, copy, or swap operations on individual elements. 🫢

All iterators and references remain valid. The end() iterator is invalidated. This is the important bit, bestie.

The Hash and KeyEqual objects must be Swappable, and they are exchanged using unqualified calls to non-member swap.

If std::allocator_traits<allocator_type>::propagate_on_container_swap::value is true, then the allocators are exchanged using an unqualified call to non-member swap. Otherwise, they are not swapped β€” and if get_allocator() != other.get_allocator(), the behavior is undefined. 🚨 (since C++11)

πŸŽ€ Parameters πŸŽ€

  • other β€” container to exchange the contents with. That’s the parameter, that’s the whole gig. πŸ’…

πŸ’– Return value πŸ’–

(none) β€” literally nothing comes back. No return value, no drama. 😌

🚨 Exceptions 🚨

Any exception thrown by the swap of the Hash or KeyEqual objects. (until C++17)

noexcept specification (since C++17):

noexcept(std::allocator_traits<Allocator>::is_always_equal::value
&& std::is_nothrow_swappable<Hash>::value
&& std::is_nothrow_swappable<key_equal>::value)

⚑ Complexity ⚑

Constant. No cap, the complexity stays constant. ⚑

🧚 Example 🧚

// Run this code

#include <iostream>
#include <string>
#include <utility>
#include <unordered_map>

// print out a std::pair
template<class Os, class U, class V>
Os& operator<<(Os& os, const std::pair<U, V>& p)
{
    return os << p.first << ':' << p.second;
}

// print out a container
template<class Os, class Co>
Os& operator<<(Os& os, const Co& co)
{
    os << '{';
    for (auto const& i : co)
        os << ' ' << i;
    return os << " }\n";
}

int main()
{
    std::unordered_multimap<std::string, std::string>
        m1{{"Ξ³", "gamma"}, {"Ξ²", "beta"}, {"Ξ±", "alpha"}, {"Ξ³",
            "gamma"}},
        m2{{"Ξ΅", "epsilon"}, {"Ξ΄", "delta"}, {"Ξ΅", "epsilon"}};

    const auto& ref = *(m1.begin());
    const auto iter = std::next(m1.cbegin());

    std::cout << "──────── before swap ────────\n"
              << "m1: " << m1 << "m2: " << m2 << "ref: " << ref
              << "\niter: " << *iter << '\n';

    m1.swap(m2);

    std::cout << "──────── after swap ────────\n"
              << "m1: " << m1 << "m2: " << m2 << "ref: " << ref
              << "\niter: " << *iter << '\n';

    // Note that every iterator referring to an element in one
    // container before the swap refers to the same element in the
    // other container after the swap. Same is true for references.
}

πŸ’¬ Possible output πŸ’¬

──────── before swap ────────
m1: { Ξ±:alpha Ξ²:beta Ξ³:gamma Ξ³:gamma }
m2: { Ξ΄:delta Ξ΅:epsilon Ξ΅:epsilon }
ref: Ξ±:alpha
iter: Ξ²:beta
──────── after swap ────────
m1: { Ξ΄:delta Ξ΅:epsilon Ξ΅:epsilon }
m2: { Ξ±:alpha Ξ²:beta Ξ³:gamma Ξ³:gamma }
ref: Ξ±:alpha
iter: Ξ²:beta

πŸ‘‘ See also πŸ‘‘

  • std::swap(std::unordered_multimap) β€” specializes the std::swap algorithm (C++11) (function template). Yes bestie, function template, periodt. πŸŽ€

🏷️ Category 🏷️

  • conditionally noexcept

okay that’s the whole page β€” mwah πŸ’‹ sourced from http://cppreference.com, version/date 2024.06.10, and the footer title is that same truncated std::unorde...ltimap::swap(3) vibe. Byeeee πŸ¦‹βœ¨

πŸ“¦ where this page comes from