std::swap(std::priority_queue)
来自cppreference.com
<tbody>
</tbody>
<tbody class="t-dcl-rev ">
</tbody><tbody>
</tbody>
| 在标头 <queue> 定义
|
||
template< class T, class Container, class Compare > void swap( std::priority_queue<T, Container, Compare>& lhs, std::priority_queue<T, Container, Compare>& rhs ); |
(C++11 起) (C++17 前) |
|
template< class T, class Container, class Compare > void swap( std::priority_queue<T, Container, Compare>& lhs, std::priority_queue<T, Container, Compare>& rhs ) noexcept(/* 见下文 */); |
(C++17 起) | |
为 std::priority_queue 特化了 std::swap 算法。交换 lhs 与 rhs 的内容。调用 lhs.swap(rhs)。
|
此重载只有在 |
(C++17 起) |
参数
| lhs, rhs | - | 要交换内容的容器 |
返回值
(无)
复杂度
与交换各底层容器相同。
异常
|
noexcept 说明:
noexcept(noexcept(lhs.swap(rhs))) |
(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);
// 打印 swap 之前的状态
print("Alice:", alice);
print("Bobby:", bob);
std::cout << "-- SWAP\n";
std::swap(alice, bob);
// 打印 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) |
交换内容 (公开成员函数) |