std::queue::swap(3)
β¨ NAME β¨
bestie, welcome to std::queue::swap(3) from the C++ Standard Libary β¨ β the manual name is literally std::queue::swap(3), and the NAME line is exactly std::queue::swap - std::queue::swap π
. It's giving "swap your queue's contents" main character energy, no cap.
π¦ Synopsis
void swap( 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);
π Parameters
- other β container adaptor to exchange the contents with π
πΈ Return value
(none) β literally nothing, bestie π«Ά
π₯ Exceptions
noexceptspecification,(since C++11)and(until C++17):
noexcept(noexcept(swap(c, other.c)))
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.
noexceptspecification,(since C++17):
noexcept(std::is_nothrow_swappable_v<Container>)
π¦ Complexity
Same as underlying container (typically constant) β¨
π Notes
Some implementations (e.g. libc++) provide the swap member function as an extension to pre-C++11 modes π
π Example
okay so // Run this code β here's the whole example, bestie:
#include <iostream>
#include <concepts>
#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::queue s1(std::move(v1));
std::queue s2(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 π:
LWG 2456β applied toC++11; behavior as published: thenoexceptspecification is ill-formed; correct behavior: made to work π
π See also
std::swap(std::queue)specializes thestd::swapalgorithm β(C++11)(function template) β
π Category
conditionally noexcept
okay bestie, that's the whole page π β source/version: http://cppreference.com, dated 2024.06.10 πΈ
π¦ 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