C++教程
C++控制语句
C++函数
C++数组
C++指针
C++对象
C++继承
C++多态
C++抽象
C++常用
C++ STL教程
C++迭代器
C++程序

C++ multiset swap()

C++ multiset swap()

C++ multiset swap()函数用于交换(或交换)两个 multiset 但两个 multiset 的内容必须为相同类型,尽管大小可能有所不同。

语法

void swap (multiset& x);

参数

x : 用于交换内容的 multiset 容器。

返回值

复杂度

常量。

迭代器有效性

全部在两个 multiset 容器中引用元素的引用,迭代器和指针仍然有效,但是现在在另一个 multiset 容器中引用元素并对其进行迭代。

数据竞争

容器和x均被修改。

异常安全性

如果引发异常,则对容器没有影响。

示例1

让我们看一个简单的示例,将一个 multiset 的元素交换为另一个:
#include <iostream>
#include <set>
using namespace std;
int main(void) {
   multiset<int> m1 = {1,2,3,4,5,3};
   multiset<int> m2;
   m2.swap(m1);
   cout << "Multiset m2 contains following elements" << endl;
   for (auto it = m2.begin(); it != m2.end(); ++it)
      cout << *it<< endl;
   return 0;
}
输出:
Multiset m2 contains following elements
1
2
3
3
4
5
在上面的示例中, multiset m1有五个元素,而m2为空。当您将m1交换为m2时,m1的所有元素都将交换为m2、

示例2

让我们看一个简单的示例来交换两个 multiset 的内容:
#include <iostream>
#include <set>
using namespace std;
int main ()
{
  int myints[]={19,72,4,36,20,20};
  multiset<int> first (myints,myints+3);     // 4,19,72
  multiset<int> second (myints+3,myints+6);  // 20,20,36
  first.swap(second);
  cout << "first contains:";
  for (multiset<int>::iterator it=first.begin(); it!=first.end(); ++it)
    cout << ' ' << *it;
  cout << '\n';
  cout << "second contains:";
  for (multiset<int>::iterator it=second.begin(); it!=second.end(); ++it)
    cout << ' ' << *it;
  cout << '\n';
  return 0;
}
输出:
first contains: 20 20 36
second contains: 4 19 72

示例3

让我们看一个简单的示例来交换两个 multiset 的内容:
#include<iostream>
#include<set>
using namespace std;
 
int main()
{
    // Take any two multisets
    multiset<char> multiset1, multiset2;
    
    multiset1 = {'a','b','c','d','c'}; 
    multiset2 = {'x','y','z','y'};
 
    // Swap elements of multisets
    swap(multiset1, multiset2);
 
    // Print the elements of multisets
    cout << "multiset1:\n";
    for (auto it = multiset1.begin(); it != multiset1.end(); it++)
        cout << "\t" << *it<< '\n';
 
    cout << "multiset2:\n";
    for (auto it = multiset2.begin(); it != multiset2.end(); it++)
        cout << "\t" << *it<< '\n';
 
    return 0;
}
输出:
multiset1:
  x
  y
  y
  z
multiset2:
  a
  b
  c
  c
  d
在上面的示例中,另一种形式的swap()函数用于交换两个 multiset 的内容。

示例4

让我们看看简单的例子:
#include <set>  
#include <iostream>  
  
int main( )  
{  
   using namespace std;  
   multiset <int> s1, s2, s3;  
   multiset <int>::iterator s1_Iter;  
  
   s1.insert( 10 );  
   s1.insert( 20 );  
   s1.insert( 10 );  
   s2.insert( 100 );  
   s2.insert( 200 );  
   s3.insert( 200 );  
  
   cout << "The original multiset 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, multiset 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, multiset s1 is:";  
   for ( s1_Iter = s1.begin( ); s1_Iter != s1.end( ); s1_Iter++ )  
      cout << " " << *s1_Iter;  
   cout   << "." << endl;  
}  
输出:
The original multiset s1 is: 10 10 20.
After swapping with s2, multiset s1 is: 100 200.
After swapping with s3, multiset s1 is: 200.
昵称: 邮箱:
Copyright © 2022 立地货 All Rights Reserved.
备案号:京ICP备14037608号-4