std::swap(std::priority_queue)
来自 cppreference.cn
< cpp | container | priority queue
定义于头文件 <queue> |
||
template< class T, class Container, class Compare > void swap( std::priority_queue<T, Container, Compare>& lhs, |
(since C++11) (until C++17) |
|
template< class T, class Container, class Compare > void swap( std::priority_queue<T, Container, Compare>& lhs, |
(since C++17) | |
此重载仅若 std::is_swappable_v<Container> 与 std::is_swappable_v<Compare> 均为 true 才参与重载决议。 |
(since C++17) |
内容 |
[编辑] 参数
lhs, rhs | - | 要交换内容的容器 |
[编辑] 返回值
(无)
[编辑] 复杂度
与交换底层容器相同。
异常
noexcept 规范:
noexcept(noexcept(lhs.swap(rhs))) |
(since C++17) |
注解
虽然容器适配器的 std::swap 重载于 C++11 引入,但容器适配器已经可以通过 C++98 中的 std::swap 交换。此类 std::swap 调用通常具有线性时间复杂度,但可能提供更佳的复杂度。
[编辑] 示例
运行此代码
#include <algorithm> #include <iostream> #include <queue> int main() { std::priority_queue<int> alice; std::priority_queue<int> bob; auto print = [](const auto& title, const auto& cont) { std::cout << title << " size=" << cont.size(); std::cout << " top=" << cont.top() << '\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("Bobby:", bob); std::cout << "-- SWAP\n"; std::swap(alice, bob); // Print state after swap print("Alice:", alice); print("Bobby:", bob); }
输出
Alice: size=3 top=3 Bobby: size=4 top=10 -- SWAP Alice: size=4 top=10 Bobby: size=3 top=3
[编辑] 参见
(C++11) |
交换内容 (公开成员函数) |