std::priority_queue<T,Container,Compare>::swap
From cppreference.com
void swap( priority_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);
swap(comp, other.comp);
.
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 <functional>
#include <queue>
#include <string>
#include <vector>
int main()
{
std::vector<std::string> v1{"1","2","3","4"},
v2{"Ɐ","B","Ɔ","D","Ǝ"};
std::priority_queue s1(std::less<>(), std::move(v1));
std::priority_queue s2(std::less<>(), std::move(v2));
std::println("s1: {}", s1);
std::println("s2: {}", s2);
s1.swap(s2);
std::println("s1: {}", s1);
std::println("s2: {}", s2);
}
Possible output:
s1: ["4", "2", "3", "1"]
s2: ["Ɐ", "Ǝ", "Ɔ", "D", "B"]
s1: ["Ɐ", "Ǝ", "Ɔ", "D", "B"]
s2: ["4", "2", "3", "1"]
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
| overloads the std::swap algorithm (function template) |