swap
描述 (Description)
C ++函数std::deque::swap()将第一个deque的内容与另一个deque交换。 如有必要,此功能可更改双端队列的大小。
声明 (Declaration)
以下是std :: deque :: swap()函数形式std :: deque header的声明。
C++98
template <class T, class Alloc>
void swap (deque<T,Alloc>& first, deque<T,Alloc>& second);
参数 (Parameters)
first - 第一个deque对象。
second - 第二个deque对象。
返回值
没有。
异常 (Exceptions)
该成员函数从不抛出异常。
时间复杂
线性即O(n)
例子 (Example)
以下示例显示了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;
swap(d1, 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