std::priority_queue::swap(3)
π 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 toC++11; behavior as published: thenoexceptspecification is ill-formed; correct behavior: made to work.
πΈ See also πΈ
std::swap(std::priority_queue)specializes thestd::swapalgorithm (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
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms