std::swap(s...rity_queue)(3)
π
std::swap(std::priority_queue) π
okay bestie, welcome to the C++ Standard Libary manual page for std::swap(std::priority_queue) β yes, the spelling in the header is Libary, not me π. This one is all about swapping two std::priority_queues and it's lowkey iconic.
β¨ NAME β¨
std::swap(std::priority_queue) β std::swap(std::priority_queue). That's the name, periodt.
π¦ Synopsis π¦
Defined in header <queue>
First overload β (since C++11), (until C++17):
template< class T, class Container, class Compare >
void swap( std::priority_queue<T, Container, Compare>& lhs,
std::priority_queue<T, Container, Compare>& rhs );
Second overload β (since C++17):
template< class T, class Container, class Compare >
void swap( std::priority_queue<T, Container, Compare>& lhs,
std::priority_queue<T, Container, Compare>& rhs )
noexcept(/* see below */);
Specializes the std::swap algorithm for std::priority_queue. Swaps the contents of lhs and rhs. Calls lhs.swap(rhs). That's the whole vibe.
Highkey important: this overload participates in overload resolution only if std::is_swappable_v<Container> and std::is_swappable_v<Compare> are both true. (since C++17)
π Parameters π
lhs,rhsβ containers whose contents to swap
π«Ά Return value π«Ά
none.
π₯ Complexity π₯
Same as swapping the underlying container. Simple as that.
π Exceptions π
- noexcept specification:
(since C++17)noexcept(noexcept(lhs.swap(rhs)))
π Notes π
Okay history tea: although the overloads of std::swap for container adaptors are introduced in C++11, container adaptors can already be swapped by std::swap in C++98. Such calls to std::swap usually have linear time complexity, but better complexity may be provided.
no cap, that C++98 note is the lil plot twist π«Ά
πΈ Example πΈ
The original has the lil // Run this code marker, and here's the exact code:
#include <algorithm>
#include <iostream>
#include <queue>
int main()
{
std::priority_queue<int> alice;
std::priority_queue<int> bob;
auto print = [](const auto& title, const auto& cont)
{
std::cout << title << " size=" << cont.size();
std::cout << " top=" << cont.top() << '\n';
};
for (int i = 1; i < 4; ++i)
alice.push(i);
for (int i = 7; i < 11; ++i)
bob.push(i);
// Print state before swap
print("Alice:", alice);
print("Bob :", bob);
std::cout << "-- SWAP\n";
std::swap(alice, bob);
// Print state after swap
print("Alice:", alice);
print("Bob :", bob);
}
π Output π
Alice: size=3 top=3
Bob : size=4 top=10
-- SWAP
Alice: size=4 top=10
Bob : size=3 top=3
π See also π
swapβ swaps the contents(C++11)(public member function)
π·οΈ Category π·οΈ
* conditionally noexcept
πΈ okay that's the whole page! source/version/date: http://cppreference.com Β· 2024.06.10 Β· page std::swap(s...rity_queue)(3) β bye bestie, hydrate and swap responsibly π
π¦ 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