swap
描述 (Description)
C ++函数std::priority_queue::swap()交换两个priority_queues的内容。
声明 (Declaration)
以下是std :: priority_queue :: swap()函数形式std :: queue header的声明。
C++11
template <class T, class Container, class Compare>
void swap (queue<T,Container,Compare>& q1,
queue <T,Container,Compare>& q2) noexcept;
参数 (Parameters)
q1 - 第一个priority_queue对象。
q2 - 第二个priority_queue对象。
返回值
没有。
异常 (Exceptions)
该成员函数从不抛出异常。
时间复杂
线性即O(n)
例子 (Example)
以下示例显示了std :: priority_queue :: swap()函数的用法。
#include <iostream>
#include <queue>
using namespace std;
int main(void) {
auto it1 = {3, 1, 5, 2, 4};
auto it2 = {2, 4};
priority_queue<int> q1(less<int>(), it1);
priority_queue<int> q2(less<int>(), it2);
swap(q1, q2);
cout << "Content of q1" << endl;
while (!q1.empty()) {
cout << q1.top() << endl;
q1.pop();
}
cout << "Content of q2" << endl;
while (!q2.empty()) {
cout << q2.top() << endl;
q2.pop();
}
return 0;
}
让我们编译并运行上面的程序,这将产生以下结果 -
Content of q1
4
2
Content of q2
5
4
3
2
1