std::swap(std::queue)(3)
π NAME π
std::swap(std::queue) β bestie, itβs literally std::swap(std::queue). Weβre in the C++ Standard Libary manual (3), and this page is all about swapping two std::queue containers with main character energy π
β¨
π¦ Synopsis π¦
Defined in header <queue> β noted, bestie π«Ά
template< class T, class Container >
(since C++11)
void swap( std::queue<T, Container>& lhs, (until C++17)
std::queue<T, Container>& rhs );
template< class T, class Container >
void swap( std::queue<T, Container>& lhs, (since C++17)
std::queue<T, Container>& rhs )
noexcept(/* see below */);
okay so this thing specializes the std::swap algorithm for std::queue π₯ It swaps the contents of lhs and rhs, and it calls lhs.swap(rhs). no cap.
Also heads up: (since C++17), this overload only participates in overload resolution if std::is_swappable_v<Container> is true π
πΈ Parameters πΈ
lhs, rhsβ containers whose contents to swap. thatβs the whole assignment, bestie π
π Return value π
(none) β literally no return value, periodt π
π¦ Complexity π¦
Same as swapping the underlying container. lowkey straightforward, highkey iconic β¨
π₯ Exceptions π₯
noexcept specification: (since C++17)
noexcept(noexcept(lhs.swap(rhs)))
thatβs the vibe check for exceptions, and it passed π«Ά
π Notes π
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. so yeah, itβs giving βworks before it was officially coolβ πβ¨
π Example π
// Run this code
#include <algorithm>
#include <iostream>
#include <queue>
int main()
{
std::queue<int> alice;
std::queue<int> bob;
auto print = [](const auto& title, const auto& cont)
{
std::cout << title << " size=" << cont.size();
std::cout << " front=" << cont.front();
std::cout << " back=" << cont.back() << '\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 front=1 back=3
Bob : size=4 front=7 back=10
-- SWAP
Alice: size=4 front=7 back=10
Bob : size=3 front=1 back=3
slay, Alice and Bob really traded places and ate π π₯
π See also π
swapβ swaps the contents;(C++11)(public member function) π«Ά
π Category π
* conditionally noexcept
okay bye bestie π this page is from http://cppreference.com, dated 2024.06.10, and the page id is std::swap(std::queue)(3) πβ¨π¦
π¦ 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