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


C++ set swap()用法及代碼示例

C++ swap() 函數用於交換(或交換)兩個集合的內容,但盡管大小可能不同,但兩個集合的類型必須相同。

用法

void swap (set& x);

參數

x:set 容器來交換內容。

返回值

複雜度

恒定。

迭代器有效性

引用兩個集合容器中元素的所有引用、迭代器和指針仍然有效,但現在引用另一個集合容器中的元素,並在其中迭代。

數據競爭

容器和 x 都被修改了。

異常安全

如果拋出異常,對容器沒有影響。

例子1

讓我們看一個簡單的例子,將一個集合的元素交換到另一個:

#include <iostream>
#include <set>

using namespace std;

int main(void) {
   set<int> m1 = {1,2,3,4,5};


   set<int> m2;

   m2.swap(m1);

   cout << "Set contains following elements" << endl;

   for (auto it = m2.begin(); it != m2.end(); ++it)
      cout << *it<< endl;

   return 0;
}

輸出:

Set contains following elements
1
2
3
4
5

在上麵的例子中,集合 m1 有五個元素,而 m2 是空的。當您將 m1 交換為 m2 時,m1 的所有元素都將交換為 m2。

例子2

看一個簡單的例子來交換兩個集合的內容:

#include <iostream>
#include <set>

using namespace std;

 int main () {
   int myints[] = {10,20,30,40,50,60};
   set<int> first (myints,myints+3);
   set<int> second (myints+3,myints+6);  

   first.swap(second);

   cout << "first set contains:";
   for (set<int>::iterator it = first.begin(); it!=first.end(); ++it)
      cout << ' ' << *it;
   cout << '\n';

   cout << "second set contains:";
   for (set<int>::iterator it = second.begin(); it!=second.end(); ++it)
      cout << ' ' << *it;
   cout << '\n';

   return 0;
}

輸出:

first set contains:40 50 60
second set contains:10 20 30

例子3

讓我們看一個簡單的例子來交換兩個集合的內容:

#include<iostream>
#include<set>
using namespace std;
 
int main()
{
    // Take any two sets
    set<char> set1, set2;
    
    set1 = {'a','b','c','d'}; 
    set2 = {'x','y','z'};
 
    // Swap elements of sets
    swap(set1, set2);
 
    // Print the elements of sets
    cout << "set1:\n";
    for (auto it = set1.begin(); it != set1.end(); it++)
        cout << "\t" << *it<< '\n';
 
    cout << "set2:\n";
    for (auto it = set2.begin(); it != set2.end(); it++)
        cout << "\t" << *it<< '\n';
 
    return 0;
}

輸出:

set1:
	x
	y
	z
set2:
	a
	b
	c
	d

在上麵的例子中,另一種形式的 swap() 函數用於交換兩個集合的內容。

示例 4

讓我們看一個簡單的例子:

#include <set>  
#include <iostream>  
  
int main( )  
{  
   using namespace std;  
   set <int> s1, s2, s3;  
   set <int>::iterator s1_Iter;  
  
   s1.insert( 10 );  
   s1.insert( 20 );  
   s1.insert( 30 );  
   s2.insert( 100 );  
   s2.insert( 200 );  
   s3.insert( 300 );  
  
   cout << "The original set s1 is:";  
   for ( s1_Iter = s1.begin( ); s1_Iter != s1.end( ); s1_Iter++ )  
      cout << " " << *s1_Iter;  
   cout   << "." << endl;  
  
   // This is the member function version of swap  
   s1.swap( s2 );  
  
   cout << "After swapping with s2, list s1 is:";  
   for ( s1_Iter = s1.begin( ); s1_Iter != s1.end( ); s1_Iter++ )  
      cout << " " << *s1_Iter;  
   cout  << "." << endl;  
  
   // This is the specialized template version of swap  
   swap( s1, s3 );  
  
   cout << "After swapping with s3, list s1 is:";  
   for ( s1_Iter = s1.begin( ); s1_Iter != s1.end( ); s1_Iter++ )  
      cout << " " << *s1_Iter;  
   cout   << "." << endl;  
}

輸出:

The original set s1 is:10 20 30.
After swapping with s2, list s1 is:100 200.
After swapping with s3, list s1 is:300.





相關用法


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