目录

set::swap

描述 (Description)

它通过x的内容交换容器的内容。

声明 (Declaration)

以下是std :: set :: swap在各种C ++版本中的工作方式。

C++98

void swap (set& x);

C++11

void swap (set& x);

返回值

没有

异常 (Exceptions)

它从不抛出异常。

时间复杂

时间复杂性是不变的。

例子 (Example)

以下示例显示了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
↑回到顶部↑
WIKI教程 @2018