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

C++ multimap swap(multimap)

C++ multimap swap(multimap)

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

语法

template <class Key, class T, class Compare, class Alloc>
  void swap (multimap<Key,T,Compare,Alloc>& x, multimap<Key,T,Compare,Alloc>& y);

参数

x : 第一个 multimap 对象。
y : 的第二个 multimap 对象

返回值

复杂度

常量。

迭代器有效性

引用两个容器中元素的所有迭代器,引用和指针均保持有效。
请注意,最终迭代器未引用

数据竞争

容器x和y均被修改。
该调用未访问任何包含的元素。

异常安全性

此函数不会引发异常。

示例1

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

示例2

让我们看一个简单的示例,该示例交换两个 multimap 的内容:
#include <iostream>
#include <map>
using namespace std;
int main ()
{
  multimap<char,int> multimap1,multimap2;
  multimap1 = { {'x', 100},
                {'y', 200}  };
  multimap2= { {'a', 110},
               {'c', 220},
               {'c', 330} };
  swap(multimap1,multimap2);
  cout << "multimap1 contains:\n";
  for (multimap<char,int>::iterator it=multimap1.begin(); it!=multimap1.end(); ++it)
    cout << it->first << " => " << it->second << '\n';
  cout << "multimap2 contains:\n";
  for (multimap<char,int>::iterator it=multimap2.begin(); it!=multimap2.end(); ++it)
    cout << it->first << " => " << it->second << '\n';
  return 0;
}
输出:
multimap1 contains:
a => 110
c => 220
c => 330
multimap2 contains:
x => 100
y => 200
在上面的示例中,两个 multimap (即multimap1和multimap2)的内容相互交换。

示例3

让我们看一个简单的示例交换两个 multimap 的内容:
#include <iostream>
#include <map>
using namespace std;
template <class Multimap>
void print(const char* name, const Multimap& m)
{
  cout << name << " : {";
  for (const auto& x : m) {
    cout << "[" << x.first << "," << x.second << "], ";
  }
  cout << "}" << endl;
}
int main()
{
  multimap<int, char> m1;
  m1.insert(std::make_pair(10, 'a'));
  m1.insert(std::make_pair(20, 'b'));
  m1.insert(std::make_pair(10, 'c'));
  multimap<int, char> m2;
  m2.insert(std::make_pair(5,  'd'));
  m2.insert(std::make_pair(15, 'e'));
//Swap m1 and m2
  swap(m1, m2);
  print("m1", m1);
  print("m2", m2);
}
输出:
m1 : {[5,d], [15,e], }
m2 : {[10,a], [10,c], [20,b], }

示例4

让我们看一个简单的示例:
#include <iostream>
#include <string>
#include <map>
using namespace std;
void show(const char *msg, multimap<string, int> mp);
int main() {
  multimap<string, int> m1, m2;
  m1.insert(pair<string, int>("A", 100));
  m1.insert(pair<string, int>("G", 300));
  m1.insert(pair<string, int>("B", 200));
  // 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 multimap<string, int> by using an iterator.
void show(const char *msg, multimap<string, int> mp) {
  multimap<string, int>::iterator itr;
  cout << msg << endl;
  for(itr=mp.begin(); itr != mp.end(); ++itr)
    cout << "  " << itr->first << ", " << itr->second << endl;
  cout << endl;
}
输出:
Exchange m1 and m2.
Contents of m2: 
  A, 100
  B, 200
  G, 300
Contents of m1: 
m1 is now empty.
在上面的示例中,将 multimap m1的内容交换到 multimap m2,并且在清除了交换m1 multimap 后已将其清除。

昵称: 邮箱:
Copyright © 2022 立地货 All Rights Reserved.
备案号:京ICP备14037608号-4