描述
C++ 函數std::priority_queue::swap()將 priority_queue 的內容與另一個 priority_queue 的內容交換。
聲明
以下是 std::priority_queue::swap() 函數形式 std::queue 頭的聲明。
C++11
void swap (priority_queue& x) noexcept;
參數
x- 另一個相同類型的 prioriy_queue 對象。
返回值
沒有任何。
異常
此成員函數從不拋出異常。
時間複雜度
常數,即 O(1)
示例
下麵的例子展示了 std::priority_queue::swap() 函數的用法。
#include <iostream>
#include <queue>
using namespace std;
int main(void) {
auto it1 = {3, 1, 5, 2, 4};
auto it2 = {5, 2, 4};
priority_queue<int> q1(less<int>(), it1);
priority_queue<int> q2(less<int>(), it2);
q1.swap(q2);
cout << "Contents of q1 after swap operation" << endl;
while (!q1.empty()) {
cout << q1.top() << endl;
q1.pop();
}
cout << "Contents of q2 after swap operation" << endl;
while (!q2.empty()) {
cout << q2.top() << endl;
q2.pop();
}
return 0;
}
讓我們編譯並運行上麵的程序,這將產生以下結果——
Contents of q1 after swap operation 5 4 2 Contents of q2 after swap operation 5 4 3 2 1
相關用法
- C++ Queue swap()用法及代碼示例
- C++ Queue size()用法及代碼示例
- C++ Queue emplace()用法及代碼示例
- C++ Queue push()用法及代碼示例
- C++ Queue top()用法及代碼示例
- C++ Queue front()用法及代碼示例
- C++ Queue priority_queue()用法及代碼示例
- C++ Queue back()用法及代碼示例
- C++ Queue queue()用法及代碼示例
- C++ Queue empty()用法及代碼示例
- C++ Queue pop()用法及代碼示例
- C++ unordered_map cbegin用法及代碼示例
- C++ map lower_bound()用法及代碼示例
- C++ Unordered_multimap reserve()用法及代碼示例
- C++ list assign()用法及代碼示例
- C++ std::max()用法及代碼示例
- C++ std::string::push_back()用法及代碼示例
- C++ Array swap()用法及代碼示例
- C++ multimap key_comp()用法及代碼示例
- C++ Deque erase()用法及代碼示例
注:本文由純淨天空篩選整理自 C++ Queue Library - swap() Function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。