无涯教程-C++ Multimap - swap()函数

81 阅读3分钟

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

swap - 语法

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

swap - 参数

x :第一个multimap对象。

y :第二个相同类型的multimap对象。

swap - 返回值

没有

swap - 例子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

在上面的示例中,multimapm1具有五个元素,而m2为空。当您将m1交换为m2时,m1的所有元素都将交换为m2。

swap - 例子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 << 
;

  cout << "multimap2 contains:\n";
  for (multimap<char,int>::iterator it=multimap2.begin(); it!=multimap2.end(); ++it)
    cout << it->first << " => " << it->second << 
;

  return 0;
}

输出:

multimap1 contains:
a => 110
c => 220
c => 330

multimap2 contains: x => 100 y => 200

在上面的例子中,两个multimap的内容,即multimap1和multimap2相互交换。

swap - 例子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], }

swap - 例子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已被清除。

参考链接

www.learnfk.com/c++/cpp-mul…