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

C++ swap(multiset)

C++ swap(multiset)

C++ Multiset swap(multiset)是非成员函数,位于C++。这用于交换(或交换)两个 multiset (即x和y)的内容,但是两个 multiset 都必须是相同类型,尽管大小可能会有所不同。

语法

template <class T, class Compare, class Allocator>
void swap (multiset<T,Compare,Allocator>& x, multiset<T,Compare,Allocator>& y);

参数

x : 第一个 multiset 对象。
y : 相同类型的第二个 multiset 对象。

返回值

复杂度

常量。

迭代器有效性

所有迭代器,引用和指针均引用这两个元素容器保持有效。
请注意,最终迭代器不会引用元素,并且可能无效。

数据竞争

两个容器x和y被修改。
该调用没有访问任何包含的元素。

异常安全性

此函数不会引发异常。

示例1

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

示例2

让我们看一个简单的示例,交换两个 multiset 的内容:
#include <iostream>
#include <set>
using namespace std;
int main ()
{
  multiset<int> multiset1,multiset2;
  multiset1= {100,200,100};
  multiset2 = {110, 220, 330};
  swap(multiset1,multiset2);
  cout << "multiset1 contains:\n";
  for (multiset<int>::iterator it=multiset1.begin(); it!=multiset1.end(); ++it)
    cout << *it<< '\n';
  cout << "multiset2 contains:\n";
  for (multiset<int>::iterator it=multiset2.begin(); it!=multiset2.end(); ++it)
    cout << *it<< '\n';
  return 0;
}
输出:
multiset1 contains:
110
220
330
multiset2 contains:
100
100
200
在上面的示例中,两个 multiset (即multiset1和multiset2)的内容相互交换。

示例3

让我们看一个简单的示例交换两个 multiset 的内容:
#include <iostream>
#include <set>
using namespace std;
 int main ()
{
  int myints[]={12,75,10,17,20,17};
  multiset<int> first (myints,myints+3);     
  multiset<int> second (myints+3,myints+6);  
  swap(first,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: 17 17 20
second contains: 10 12 75

示例4

让我们看一个简单的示例:
#include <iostream>
#include <string>
#include <set>
using namespace std;
void show(const char *msg, multiset<int> mp);
int main() {
  multiset<int> m1, m2;
  m1.insert(100);
  m1.insert(300);
  m1.insert(100);
  // Exchange the contents of m1 and m2.
  cout << "Exchange m1 and m2.\n";
  swap(m1,m2);
  show("Contents of m2: ", m2);
  show("Contents of m1: ", m1);
 // Clear m1.
  m1.clear();
  if(m1.empty()) cout << "m1 is now empty.";
  return 0;
}
// Display the contents of a multiset<string, int> by using an iterator.
void show(const char *msg, multiset<int> mp) {
  multiset<int>::iterator itr;
  cout << msg << endl;
  for(itr=mp.begin(); itr != mp.end(); ++itr)
    cout << "  " << *itr<< endl;
  cout << endl;
  
}
输出:
Exchange m1 and m2.
Contents of m2: 
  100
  100
  300
Contents of m1: 
m1 is now empty.
在上面的示例中,将 multiset m1的内容交换为 multiset m2,并在交换了m1 multiset 后将其清除。
昵称: 邮箱:
Copyright © 2022 立地货 All Rights Reserved.
备案号:京ICP备14037608号-4