std::priority_queue::swap(3)

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

πŸ’– std::priority_queue::swap(3) πŸ’–

okay so bestie, you clicked into the C++ Standard Libary manual and landed on std::priority_queue::swap β€” and honestly? iconic. this page is short, but it's giving tiny function with big main character energy πŸ’…βœ¨ no cap, it ate and left no crumbs.

🌸 NAME 🌸

std::priority_queue::swap β€” std::priority_queue::swap

(Yes, the name is literally the name. We love a self-aware moment. slay. 🫢 periodt.)

✨ Synopsis ✨

the whole vibe, signature first:

void swap( priority_queue& other ) noexcept(/* see below */);  (since C++11)

Exchanges the contents of the container adaptor with those of other. Effectively calls:

using std::swap;
swap(c, other.c);
swap(comp, other.comp);

πŸŽ€ Parameters πŸŽ€

  • other β€” container adaptor to exchange the contents with.

🌈 Return value 🌈

(none) β€” periodt. That's the whole return value. πŸ’–

πŸ’€ Exceptions πŸ’€

the noexcept specification is doing a whole vibe check here, and it changes with the standard (not me being dramatic, it literally changes 😭):

since C++11, until C++17 β€” the noexcept specification is:

noexcept(noexcept(swap(c, other.c)) && noexcept(swap(comp, other.comp)))

In the expression above, the identifier swap is looked up in the same manner as the one used by the C++17 std::is_nothrow_swappable trait.

since C++17 β€” the noexcept specification is:

noexcept(std::is_nothrow_swappable_v<Container> &&
         std::is_nothrow_swappable_v<Compare>)

πŸ¦‹ Complexity πŸ¦‹

Same as underlying container (typically constant). lowkey a relief πŸ“ fr fr.

πŸ’œ Notes πŸ’œ

Some implementations (e.g. libc++) provide the swap member function as an extension to pre-C++11 modes. iykyk β€” highkey an extension situation, not the main standard plotline, ngl.

πŸ”₯ Example πŸ”₯

the page says // Run this code β€” so go off, bestie:

#include <iostream>
#include <concepts>
#include <functional>
#include <queue>
#include <string>
#include <string_view>
#include <vector>

template<typename Adaptor>
requires (std::ranges::input_range<typename Adaptor::container_type>)
void print(std::string_view name, const Adaptor& adaptor)
{
    struct Printer : Adaptor // to use protected Adaptor::Container c;
    {
        void print(std::string_view name) const
        {
            std::cout << name << " [" << std::size(this->c) << "]: ";
            for (auto const& elem : this->c)
                std::cout << elem << ' ';
            std::cout << '\n';
        }
    };

    static_cast<Printer const&>(adaptor).print(name);
}

int main()
{
    std::vector<std::string> v1{"1","2","3","4"},
                             v2{"β±―","B","Ζ†","D","Ǝ"};

    std::priority_queue s1(std::less<>(), std::move(v1));
    std::priority_queue s2(std::less<>(), std::move(v2));

    print("s1", s1);
    print("s2", s2);

    s1.swap(s2);

    print("s1", s1);
    print("s2", s2);
}

🌟 Output 🌟

s1 [4]: 4 3 2 1
s2 [5]: Ǝ D Ζ† B β±―
s1 [5]: Ǝ D Ζ† B β±―
s2 [4]: 4 3 2 1

🫢 Defect reports 🫢

The following behavior-changing defect reports were applied retroactively to previously published C++ standards.

  • DR LWG 2456 β€” applied to C++11; behavior as published: the noexcept specification is ill-formed; correct behavior: made to work.

🌸 See also 🌸

  • std::swap(std::priority_queue) specializes the std::swap algorithm (C++11) (function template)

obsessed with a good cross-reference πŸ’–

πŸŽ€ Category πŸŽ€

  • conditionally noexcept

πŸ’Œ source/version/date: http://cppreference.com Β· 2024.06.10 Β· page: std::priority_queue::swap(3) β€” byeee bestie, go swap those queues πŸ’–

πŸ“¦ where this page comes from