std::queue<T,Container>::swap
From cppreference.com
void swap( queue& other ) noexcept(/* see below */);
|
(since C++11) (constexpr since C++26) |
|
Exchanges the contents of the container adaptor with those of other.
Equivalent to
using std::swap;
swap(c, other.c);
.
Parameters
| other | - | container adaptor to exchange the contents with |
Exceptions
|
noexcept specification:
In the expression above, the identifier |
(since C++11) (until C++17) |
|
noexcept specification:
|
(since C++17) |
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
Run this code
#include <print>
#include <queue>
#include <string>
#include <vector>
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));
std::println("s1: {}", s1);
std::println("s2: {}", s2);
s1.swap(s2);
std::println("s1: {}", s1);
std::println("s2: {}", s2);
}
Output:
s1: ["1", "2", "3", "4"]
s2: ["Ɐ", "B", "Ɔ", "D", "Ǝ"]
s1: ["Ɐ", "B", "Ɔ", "D", "Ǝ"]
s2: ["1", "2", "3", "4"]
Defect reports
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
| DR | Applied to | Behavior as published | Correct behavior |
|---|---|---|---|
| LWG 2456 | C++11 | the noexcept specification was ill-formed
|
made to work |
See also
(C++11) |
overloads the std::swap algorithm (function template) |