std::swap(std::queue)(3)

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

πŸ’– 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