(C++17) | ||||
Sequence | ||||
(C++11) | ||||
(C++26) | ||||
(C++26) | ||||
(C++11) | ||||
Associative | ||||
Unordered associative | ||||
(C++11) | ||||
(C++11) | ||||
(C++11) | ||||
(C++11) | ||||
Adaptors | ||||
(C++23) | ||||
(C++23) | ||||
(C++23) | ||||
(C++23) | ||||
Views | ||||
(C++20) | ||||
(C++23) | ||||
Tables | ||||
Iterator invalidation | ||||
Member function table | ||||
Non-member function table |
std::priority_queue
Member functions | ||||
Element access | ||||
Capacity | ||||
Modifiers | ||||
(C++23) | ||||
(C++11) | ||||
priority_queue::swap (C++11) | ||||
Non-member functions | ||||
(C++11) | ||||
Helper classes | ||||
Deduction guides(C++17) |
void swap( priority_queue& other)noexcept(/* see below */); | (since C++11) | |
other
. Effectively callsusingstd::swap;swap(c, other.c);swap(comp, other.comp);
Contents |
other | - | container adaptor to exchange the contents with |
(none)
noexcept specification: noexcept(noexcept(swap(c, other.c))&&noexcept(swap(comp, other.comp))) In the expression above, the identifier | (since C++11) (until C++17) |
noexcept specification: noexcept(std::is_nothrow_swappable_v<Container>&& std::is_nothrow_swappable_v<Compare>) | (since C++17) |
Same as underlying container (typically constant).
Some implementations (e.g. libc++) provide theswap
member function as an extension to pre-C++11 modes.
#include <iostream>#include <concepts>#include <functional>#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(autoconst& elem: this->c)std::cout<< elem<<' ';std::cout<<'\n';}}; static_cast<Printerconst&>(adaptor).print(name);} 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)); print("s1", s1); print("s2", s2); s1.swap(s2); print("s1", s1); print("s2", s2);}
Output:
s1 [4]: 4 3 2 1s2 [5]: Ǝ D Ɔ B Ɐs1 [5]: Ǝ D Ɔ B Ɐs2 [4]: 4 3 2 1
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 | thenoexcept specification is ill-formed | made to work |
specializes thestd::swap algorithm (function template)[edit] |