當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


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

相關用法


注:本文由純淨天空篩選整理自 C++ Set Library - swap Function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。