C++ STL のソート マップ

この記事では、C++ コード スニペットを使用して C++ STL でマップを並べ替えるさまざまな方法について説明しました。

このトピックを進めるには、まずマップとは何かを理解する必要があります。
Map は、C++ の STL ライブラリの一部です。これらは、キー値とマップされた値の組み合わせで要素を連想コンテナーに格納する順序付き連想配列を実装するために使用されます。

目次 :

<オール>
  • 問題の説明の概要
  • 問題の解決 (いくつかの例とコードを使用)
  • C++ STL でのマップの概要

    マップでの並べ替えは必ずしも簡単ではありません。比較関数オブジェクトが必要です。比較オブジェクトが無視された場合、デフォルトの並べ替えが行われます。
    次の方法で地図を並べ替えることができます-

    <オール>
  • 作成中にソート
  • キーによる 2 つの要素の比較
  • 値によるマップの並べ替え
  • 作成中にソート

    マップは通常、作成時にキーでソートされて作成されます。キーが const char* の場合、リテラル テキストではなく、引用符で囲まれたリテラル文字列へのポインターが並べ替えられます。作成時に文字列をキーとして並べ替えるには、文字列は文字列クラスからインスタンス化された文字列オブジェクトのリテラルである必要があります。これは、文字列ライブラリとマップ ライブラリを含める必要があることを意味します。
    コード-

    #include <iostream>
    #include <map>v
    #include <string>//u can use bits/stdc++.h as it contains all the files
    using namespace std;
    
    int main()
    {
        //declaration of map
        map<string, const char*, less<string>> Mp;
        //intialising maps
        Mp = { { "Banana", "yellow" },
          {  "Apple", "red" },
          { "Orange", "orange" } };
        //sorting of map during creation 
        for (map<string, const char*>::iterator it = Mp.begin(); it != Mp.end();             it++)
            cout << it->first << " => " << it->second << endl;
    
        return 0;
    }
    

    アウトプット-

    Apple => red
    Banana => yellow
    Orange => orange
    

    キーによる 2 つの要素の比較

    ここでは、key_compare key_comp() を使用します。このメンバー関数は、マップ コンテナーがキーを比較するために使用する比較オブジェクトのコピーを返します。比較オブジェクトは関数オブジェクトです。引数として 2 つのキーを取り、左のキーが右より小さければ true を返します。

    コード-

     #include <iostream>
     #include <map>
     #include <string>//u can use bits/stdc++.h as it contains all the files
     using namespace std;
    
     int main()
    {
        //declaration of map
        map<string, const char*, less<string>> Mp;
        //intialising maps
        Mp = { { "Banana", "yellow" },
          {  "Apple", "red" },
          { "Orange", "orange" } };
        //comparing the values
        bool bl = Mp.key_comp()("Apple", "Orange");
    
        cout << bl << endl;
    
        return 0;
    }
    

    アウトプット-

    1
    

    これは、並べ替えの基礎となるカスタム比較関数を作成するために重要です。

    値によるマップの並べ替え

    以下は、これを達成するためのさまざまな方法です-

    1.ペアのベクトルの使用 -

    マップのすべてのコンテンツを対応するペアのベクトルにコピーし、ラムダ関数を使用して 2 番目の値に従ってペアのベクトルを並べ替えます。

    コード-

       #include <bits/stdc++.h>
       using namespace std;
       bool compare(pair<string, int>& n,pair<string, int>& m)
        {
          return n.second < m.second;
        }
       void sort(map<string, int>& M)
       {
    
        // Declare vector of pairs
       vector<pair<string, int> > Ans;
    
        // Copy key-value pair from Map
       // to vector of pairs
          for (auto& i : M) 
          {
           Ans.push_back(i);
          }
    
       // Sort using function
       sort(Ans.begin(), Ans.end(), compare);
    
       // Print the sorted value
         for (auto& i : Ans) {
    
            cout << i.first << ' '
              << i.second << endl;
             }
       }
      int main()
      {
        //declaration of map
        map<string, int> Mp;
        //intialising maps
        Mp = { { "Banana", 1 },
          {  "Apple", 3 },
          { "Orange", 2 } };
         //sorting
         sort(Mp);
    
        return 0;
      }
    

    アウトプット-

    Banana 1
    Orange 2
    Apple 3
    

    2.マルチマップの使用 -

    マルチマップは、複数の要素が同じキーを持つことができるという追加のマップです。この場合、各要素が一意であるのではなく、キーと値のペアとマップされた値のペアが一意である必要があります。

    コード-

       #include <bits/stdc++.h>
       using namespace std;
       void sort(map<string, int>& M)
       {
    
        // Declare a multimap
          multimap<int, string> MM;
    
         // Insert every (key-value) pairs from
         // map M to multimap MM as (value-key)
         // pairs
          for (auto& it : M) {
          MM.insert({ it.second, it.first });
          }
    
         // Print the multimap
          for (auto& it : MM) {
             cout << it.second << ' '
             << it.first << endl;
          }
       }
     int main()
       {
        //declaration of map
        map<string, int> Mp;
        //intialising maps
        Mp = { { "Banana", 1 },
          {  "Apple", 3 },
          { "Orange", 2 } };
        //sorting
        sort(Mp);
    
        return 0;
       } 
    

    アウトプット-

      Banana 1
      Orange 2
      Apple 3
    

    OpenGenus のこの記事を読めば、C++ で Map をソートする完全なアイデアが得られるはずです。