std::multimap::swap(3)

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

πŸ¦‹ std::multimap::swap(3) β€” C++ Standard Libary πŸ¦‹

okay bestie, this is std::multimap::swap(3) from the C++ Standard Libary β€” yes, the header spells it like that, and we respect the typo πŸ’€. it's the member swap for std::multimap, and it's about to trade whole container contents. let's go. ✨

πŸ’– NAME πŸ’–

std::multimap::swap β€” std::multimap::swap (the name section is literally the name twice, and honestly? iconic. πŸ’…)

✨ Synopsis ✨

void swap( multimap& other );                            (until C++17)
void swap( 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. no cap, it just trades the whole vibe. πŸ”₯

All iterators and references remain valid. The end() iterator is invalidated.

end() said β€œnot today” and dipped. πŸ™ˆ

The Compare objects must be Swappable, and they are exchanged using unqualified call to non-member swap. (yes, β€œunqualified call to non-member swap” is the exact phrase β€” write it down, bestie. πŸŽ€)

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)

undefined behavior alert: if those allocators don’t match and the propagate value isn’t true, it’s giving β€œwe don’t know her” and you should not go there. πŸ’€

🫢 Parameters 🫢

  • other β€” container to exchange the contents with

🌸 Return value 🌸

(none) β€” literally no return value, bestie. πŸ’…

πŸ”₯ Exceptions πŸ”₯

  • Any exception thrown by the swap of the Compare objects. (until C++17)
  • noexcept specification: noexcept(std::allocator_traits<Allocator>::is_always_equal::value && std::is_nothrow_swappable<Compare>::value) (since C++17)

⭐ Complexity ⭐

Constant. that’s the whole complexity. 😌

🧚 Example 🧚

        // Run this code

        #include <iostream>
        #include <string>
        #include <utility>
        #include <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::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.
        }

🌈 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::multimap) specializes the std::swap algorithm (function template)

🏷️ Category 🏷️

  • * conditionally noexcept β€” yes, that’s the category line, star and all. πŸ’…

big mwah from http://cppreference.com, version/date 2024.06.10, on the std::multimap::swap(3) page β€” bye bestie! πŸ’–

πŸ“¦ where this page comes from