C++ Deque 库 - swap() 函数
描述
C++ 函数 std::deque::swap() 与另一个交换第一个双端队列的内容。 如果需要,此函数会更改双端队列的大小。
声明
以下是 std::deque::swap() 函数形式 std::deque 头的声明。
C++98
void swap (deque& x);
C++11
void swap (deque& x);
参数
x − 另一个相同类型的双端队列对象。
返回值
None.
异常
此成员函数从不抛出异常。
时间复杂度
常数,即 O(1)
示例
以下示例显示了 std::deque::swap() 函数的用法。
#include <iostream>
#include <deque>
using namespace std;
int main(void) {
deque<int> d1 = {1, 2, 3, 4, 5};
deque<int> d2 = {50, 60, 70};
cout << "Content of d1 before swap operation" << endl;
for (int i = 0; i < d1.size(); ++i)
cout << d1[i] << endl;
cout << "Content of d2 before swap operation" << endl;
for (int i = 0; i < d2.size(); ++i)
cout << d2[i] << endl;
cout << endl;
d1.swap(d2);
cout << "Content of d1 after swap operation" << endl;
for (int i = 0; i < d1.size(); ++i)
cout << d1[i] << endl;
cout << "Content of d2 after swap operation" << endl;
for (int i = 0; i < d2.size(); ++i)
cout << d2[i] << endl;
return 0;
}
让我们编译并运行上面的程序,这将产生以下结果 −
Content of d1 before swap operation 1 2 3 4 5 Content of d2 before swap operation 50 60 70 Content of d1 after swap operation 50 60 70 Content of d2 after swap operation 1 2 3 4 5

