描述
C++ 函數std::queue::swap()將隊列的內容與另一個隊列的內容交換x。
聲明
以下是 std::queue::swap() 函數形式 std::queue 頭的聲明。
C++11
void swap (queue& x) noexcept;
參數
x- 另一個相同類型的隊列對象。
返回值
空
時間複雜度
常數,即 O(1)
示例
下麵的例子展示了 std::queue::swap() 函數的用法。
#include <iostream>
#include <queue>
using namespace std;
int main(void) {
queue<int> q1, q2;
for (int i = 0; i < 5; ++i)
q1.push(i + 1);
for (int i = 0; i < 2; ++i)
q2.push(i + 150);
q1.swap(q2);
cout << "Content of q1 and q2 after swap operation" << endl;;
while (!q1.empty()) {
cout << q1.front() << endl;
q1.pop();
}
cout << endl << endl;
while (!q2.empty()) {
cout << q2.front() << endl;
q2.pop();
}
return 0;
}
讓我們編譯並運行上麵的程序,這將產生以下結果——
Content of q1 and q2 after swap operation 150 151 1 2 3 4 5
相關用法
- 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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。