std::swap(std::flat_map)(3)

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

πŸ’… std::swap(std::flat_map) πŸ’…

okay bestie, this page comes from the C++ Standard Libary manual β€” yes, that's the manual name, typo and all, and we're keeping it πŸ’…. It's the std::swap page for std::flat_map, and it's here to spill exactly how the swapping works. No cap.

✨ NAME ✨

std::swap(std::flat_map) β€” literally std::swap(std::flat_map).

🌸 Synopsis 🌸

Defined in header <flat_map>.

friend void swap( std::flat_map& lhs, std::flat_map& rhs ) noexcept;

(since C++23)

Specializes the std::swap algorithm for std::flat_map. Swaps the contents of lhs and rhs. Calls lhs.swap(rhs).

πŸŽ€ Parameters πŸŽ€

  • lhs, rhs β€” containers whose contents to swap.

πŸ¦‹ Return value πŸ¦‹

(none) β€” yep, nothing, zero, no return value. It just does the swap.

πŸ’œ Complexity πŸ’œ

Same as swapping the underlying containers.

πŸ”₯ Example πŸ”₯

// Run this code

        #include <algorithm>
        #include <iostream>
        #include <flat_map>

        int main()
        {
            std::flat_map<int, char> alice{{1, 'a'}, {2, 'b'}, {3, 'c'}};
            std::flat_map<int, char> bob{{7, 'Z'}, {8, 'Y'}, {9, 'X'}, {10,
       'W'}};

            auto print = [](std::pair<const int, char>& n)
            {
                std::cout << ' ' << n.first << ':' << n.second;
            };

            // Print state before swap
            std::cout << "Alice:";
            std::for_each(alice.begin(), alice.end(), print);
            std::cout << "\n" "Bob  :";
            std::for_each(bob.begin(), bob.end(), print);
            std::cout << '\n';

            std::cout << "-- SWAP\n";
            std::swap(alice, bob);

            // Print state after swap
            std::cout << "Alice:";
            std::for_each(alice.begin(), alice.end(), print);
            std::cout << "\n" "Bob  :";
            std::for_each(bob.begin(), bob.end(), print);
            std::cout << '\n';
        }

⭐ Output ⭐

        Alice: 1:a 2:b 3:c
        Bob  : 7:Z 8:Y 9:X 10:W
        -- SWAP
        Alice: 7:Z 8:Y 9:X 10:W
        Bob  : 1:a 2:b 3:c

πŸ‘‘ See also πŸ‘‘

  • swap β€” swaps the contents (public member function).

source: http://cppreference.com · version/date: 2024.06.10 · page: std::swap(std::flat_map)(3) 🫢✨

πŸ“¦ where this page comes from