C++ STL 中 Map 和 Multimap 的降序排序


通常,map 和 multimap 的預設行為是按升序儲存元素。但是,我們可以使用 greater 函式將元素儲存在降序中。

降序排列的 map

此處使用的函式:

  • m::find() – 如果找到,則返回 map 中鍵值為 'b' 的元素的迭代器,否則返回指向 end 的迭代器。

  • m::erase() – 從 map 中刪除鍵值。

  • m:: equal_range() – 返回一對迭代器。該對指的是包含容器中所有鍵等效於 key 的元素的範圍的邊界。

  • m insert() – 將元素插入 map 容器。

  • m size() – 返回 map 容器中元素的數量。

  • m count() – 返回 map 中鍵值為 'a' 或 'f' 的元素的匹配數。

示例程式碼

#include <iostream>
#include <map>
#include <string>
using namespace std;
int main () {
   map<char, int,greater <int>> m;
   map<char, int>::iterator it;
   m.insert (pair<char, int>('a', 10));
   m.insert (pair<char, int>('b', 20));
   m.insert (pair<char, int>('c', 30));
   m.insert (pair<char, int>('d', 40));
   cout<<"Size of the map: "<< m.size() <<endl;
   cout << "map contains:\n";
   for (it = m.begin(); it != m.end(); ++it)
      cout << (*it).first << " => " << (*it).second << '\n';
   for (char c = 'a'; c <= 'd'; c++) {
      cout << "There are " << m.count(c) << " element(s) with key " << c << ":";
      map<char, int>::iterator it;
      for (it = m.equal_range(c).first; it != m.equal_range(c).second; ++it)
         cout << ' ' << (*it).second;
      cout << endl;
   }
   if (m.count('a'))
      cout << "The key a is present\n";
   else
      cout << "The key a is not present\n";
   if (m.count('f'))
      cout << "The key f is present\n";
   else
      cout << "The key f is not present\n";
      it = m.find('b');
      m.erase (it);
      cout<<"Size of the map: "<<m.size()<<endl;
   cout << "map contains:\n";
   for (it = m.begin(); it != m.end(); ++it)
      cout << (*it).first << " => " << (*it).second << '\n';
   return 0;
}

輸出

Size of the map: 4
map contains:
d => 40
c => 30
b => 20
a => 10
There are 1 element(s) with key a: 10
There are 1 element(s) with key b: 20
There are 1 element(s) with key c: 30
There are 1 element(s) with key d: 40
The key a is present
The key f is not present
Size of the map: 3
map contains:
d => 40
c => 30
a => 10

降序排列的 Multimap

此處使用的函式

  • mm::find() – 如果找到,則返回 multimap 中鍵值為 'b' 的元素的迭代器,否則返回指向 end 的迭代器。

  • mm::erase() – 從 multimap 中刪除鍵值。

  • mm:: equal_range() – 返回一對迭代器。該對指的是包含容器中所有鍵等效於 key 的元素的範圍的邊界。

  • mm insert() – 將元素插入 multimap 容器。

  • mm size() – 返回 multimap 容器中元素的數量。


示例程式碼

#include <iostream>
#include <map>
#include <string>
using namespace std;
int main () {
   multimap<char, int,greater <char>> mm;
   multimap<char, int>::iterator it;
   mm.insert (pair<char, int>('a', 10));
   mm.insert (pair<char, int>('b', 20));
   mm.insert (pair<char, int>('a', 30));
   mm.insert (pair<char, int>('b', 40));
   cout<<"Size of the multimap: "<< mm.size() <<endl;
   cout << "multimap contains:\n";
   for (it = mm.begin(); it != mm.end(); ++it)
      cout << (*it).first << " => " << (*it).second << '\n';
   for (char c = 'a'; c <= 'd'; c++) {
      cout << "There are " << mm.count(c) << " elements with key " << c << ":";
      map<char, int>::iterator it;
    for (it = mm.equal_range(c).first; it != mm.equal_range(c).second; ++it)
      cout << ' ' << (*it).second;
      cout << endl;
   }
   if (mm.count('a'))
      cout << "The key a is present\n";
   else
      cout << "The key a is not present\n";
   if (mm.count('f'))
      cout << "The key f is present\n";
   else
      cout << "The key f is not present\n";
   it = mm.find('b');
   mm.erase (it);
   cout<<"Size of the multimap: "<<mm.size()<<endl;
   cout << "multiap contains:\n";
   for (it = mm.begin(); it != mm.end(); ++it)
      cout << (*it).first << " => " << (*it).second << '\n';
   return 0;
}

輸出

Size of the multimap: 4
multimap contains:
b => 20
b => 40
a => 10
a => 30
There are 2 elements with key a: 10 30
There are 2 elements with key b: 20 40
There are 0 elements with key c:
There are 0 elements with key d:
The key a is present
The key f is not present
Size of the multimap: 3
multiap contains:
b => 40
a => 10
a => 30

更新於:2019年7月30日

3K+ 閱讀量

啟動您的職業生涯

透過完成課程獲得認證

開始學習
廣告

© . All rights reserved.