C++ Set 库 - swap 函数
描述
它通过 x 的内容交换容器的内容。
声明
以下是 std::set::swap 在各种 C++ 版本中的工作方式。
C++98
void swap (set& x);
C++11
void swap (set& x);
返回值
none
异常
它从不抛出异常。
时间复杂度
Time complexity is constant.
示例
以下示例显示了 std::set::swap 的用法。
#include <iostream>
#include <set>
main () {
int myints[] = {10,20,30,40,50,60};
std::set<int> first (myints,myints+3);
std::set<int> second (myints+3,myints+6);
first.swap(second);
std::cout << "first contains:";
for (std::set<int>::iterator it = first.begin(); it!=first.end(); ++it)
std::cout << ' ' << *it;
std::cout << '\n';
std::cout << "second contains:";
for (std::set<int>::iterator it = second.begin(); it!=second.end(); ++it)
std::cout << ' ' << *it;
std::cout << '\n';
return 0;
}
上述程序将正确编译和执行。
first contains: 40 50 60 second contains: 10 20 30

