swap
描述 (Description)
C ++函数std::forward_list::swap()将第一个forward_list的内容与另一个交换。 如有必要,此函数会更改forward_list的大小。
声明 (Declaration)
以下是std :: forward_list :: swap()函数形式std :: forward_list头的声明。
C++11template
void swap (forward_list& first, forward_list& second);
参数 (Parameters)first - 第一个forward_list对象。
second - 第二个forward_list对象。
返回值
没有
异常 (Exceptions)
这个函数永远不会抛出异常。
时间复杂
线性即O(n)
例子 (Example)
以下示例显示了std :: forward_list :: swap()函数的用法。#include
#include
using namespace std;
int main(void) {
forward_list fl1 = {1, 2, 3, 4, 5};;
forward_list fl2 = {10, 20, 30};
cout << "List fl1 contents before swap operation" << endl;
for (auto it = fl1.begin(); it != fl1.end(); ++it)
cout << *it << endl;
cout << "List fl2 contents before swap operation" << endl;
for (auto it = fl2.begin(); it != fl2.end(); ++it)
cout << *it << endl;
swap(fl1, fl2);
cout << endl;
cout << "List fl1 contents after swap operation" << endl;
for (auto it = fl1.begin(); it != fl1.end(); ++it)
cout << *it << endl;
cout << "List fl2 contents after swap operation" << endl;
for (auto it = fl2.begin(); it != fl2.end(); ++it)
cout << *it << endl;
return 0;
}
让我们编译并运行上面的程序,这将产生以下结果 -List fl1 contents before swap operation
1
2
3
4
5
List fl2 contents before swap operation
10
20
30
List fl1 contents after swap operation
10
20
30
List fl2 contents after swap operation
1
2
3
4
5