C++ Set 庫 - swap 函式



描述

它用x的內容交換容器的內容。

宣告

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

C++98

void swap (set& x);

C++11

void swap (set& x);

返回值

異常

它從不丟擲異常。

時間複雜度

時間複雜度為常數。

示例

以下示例演示了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
set.htm
廣告
© . All rights reserved.