C++程式:按值排序字典


各種計算機語言中都存在被稱為字典的資料結構。字典是一種特殊的、更快速的資料結構,它基於鍵值對儲存資料。它儲存鍵值對,以便可以幾乎即時地透過鍵快速搜尋特定元素。C++ STL標準庫中包含類似字典的資料結構。這種資料結構被稱為“map”。map可以生成任意型別的鍵值對(由於我們使用的是C++,因此必須在編譯之前定義型別)。在本節中,我們將學習如何使用C++根據值對字典條目進行排序。

讓我們首先了解如何定義map資料結構。這需要在模板內部使用兩種型別。所需的庫和語法如下所示:

map資料結構的定義語法

#include <map>
map<type1, type2> mapVariable;

在本例中,要使用map資料結構,我們必須匯入“map”庫。這需要兩種資料型別。Type1表示鍵引數的資料型別,而Type2表示值型別。從map型別類派生的物件稱為mapVariable。現在讓我們看看如何根據這些關鍵因素對map進行排序。

使用鍵值對向量

在這個方法中,我們只需要建立一個鍵值對的向量(動態陣列,它是來自C++ STL的另一個元素)。然後透過建立比較函式來執行排序。然後將內容以排序的形式再次儲存到map中。

演算法

  • 將map M作為輸入

  • 定義一個動態陣列A來儲存鍵值對

  • 對於M中的每個鍵值對p,執行以下操作:

    • 將p插入到A中

  • 結束迴圈

  • 根據鍵對A進行排序

  • 建立一個空的map newMap

  • 對於A中的每個鍵值對p:

    • 將鍵值對p插入到newMap中

  • 結束迴圈

  • 返回newMap

示例

#include <iostream>
#include <vector>
#include <map>
#include <algorithm>
using namespace std;

// Create a comparator function to perform key-value pair comparison
bool compare ( pair <string, int> &a, pair <string, int> &b ){
   return a.second < b.second;
}
 
//Define sorting function to sort given dictionary or map
map <string, int> sorting( map <string, int> givenMap ){
   vector<pair <string, int> > pairVec;
   map<string, int> newMap;
   
   for ( auto& it : givenMap ) {
      pairVec.push_back( it );
   }
   
   sort( pairVec.begin(), pairVec.end(), compare);
   
   for ( auto& it : pairVec ) {
      cout << "Key: " << it.first << ", value: " << it.second << endl;
      newMap.insert( {it.first, it.second } );
   }
   return newMap;
}

void display( map <string, int>& givenMap ){
   for ( auto& it : givenMap ) {
      cout << "Key: " << it.first << ", value: " << it.second << endl;
   }
}
   
int main(){ 
   map<string, int> givenMap;
   
   givenMap = { { "Three", 3 },
        { "Two", 2 },
        { "Four", 4 },
        { "One", 1 },
        { "Five", 5 }
   };
   
   cout << "Before Sorting: " << endl;
   display( givenMap );
  
   cout << "After Sorting: " << endl;
   givenMap = sorting( givenMap ); 
}

輸出

Before Sorting: 
Key: Five, value: 5
Key: Four, value: 4
Key: One, value: 1
Key: Three, value: 3
Key: Two, value: 2
After Sorting: 
Key: One, value: 1
Key: Two, value: 2
Key: Three, value: 3
Key: Four, value: 4
Key: Five, value: 5

我們已經完成了排序。如果我們將最終結果儲存在map中,我們將不會看到排序前後有任何區別,這是因為map資料結構通常以鍵的排序形式儲存資料。這裡我們使用向量根據值對它們進行排序。如果我們直接從向量中列印它們,則可以找到順序。

使用鍵值對集合

可以使用另一種資料結構——集合來對map資料結構中的鍵值對進行排序。集合資料結構以排序的順序儲存資料。因此,在將元素新增到集合後,不再需要再次進行排序。為了更好地理解,讓我們看看演算法。

演算法

  • 將map M作為輸入

  • 定義一個集合S來儲存鍵值對

  • 對於M中的每個鍵值對p,執行以下操作:

    • 將p插入到S中

  • 結束迴圈

  • 建立一個空的map newMap

  • 對於S中的每個鍵值對p:

    • 將鍵值對p插入到newMap中

  • 結束迴圈

  • 返回newMap

示例

#include <iostream>
#include <set>
#include <map>
#include <algorithm>
using namespace std;

// Create a comparator function to perform key-value pair comparison
struct compare {
   template <typename T>
   
   bool operator()(const T& a, const T& b) const
   {
      if (a.second != b.second) {
         return a.second < b.second;
      }
      return a.first < b.first;
   }
};

//Define sorting function to sort given dictionary or map
map <string, int> sorting( map <string, int> givenMap ){
   set<pair <string, int>, compare> pairSet( givenMap.begin(), givenMap.end() );
   map<string, int> newMap;
   
   for ( auto& it : givenMap ) {
      pairSet.insert( it );
   }
   
   for ( auto& it : pairSet ) {
      cout << "Key: " << it.first << ", value: " << it.second << endl;
      newMap.insert( {it.first, it.second } );
   }
   return newMap;
}

void display( map <string, int>& givenMap ){
   for ( auto& it : givenMap ) {
      cout << "Key: " << it.first << ", value: " << it.second << endl;
   }
}
   
int main(){ 
   map<string, int> givenMap;
   
   givenMap = { { "Three", 3 },
        { "Two", 2 },
        { "Four", 4 },
        { "One", 1 },
        { "Five", 5 }
   };
   
   cout << "Before Sorting: " << endl;
   display( givenMap );
  
   cout << "After Sorting: " << endl;
   givenMap = sorting( givenMap ); 
}

輸出

Before Sorting: 
Key: Five, value: 5
Key: Four, value: 4
Key: One, value: 1
Key: Three, value: 3
Key: Two, value: 2
After Sorting: 
Key: One, value: 1
Key: Two, value: 2
Key: Three, value: 3
Key: Four, value: 4
Key: Five, value: 5

結論

在這篇文章中,我們看到了兩種不同的方法來對字典資料結構(在C++中稱為map)進行排序,並按值進行排序。由於map是雜湊對映,因此它們的鍵的資料是使用雜湊演算法儲存的。雖然鍵是唯一的,但不同鍵的值可以相同。我們使用集合和向量排序(向量和集合包含鍵值對)對它們進行了排序。每對都有兩種不同的型別。值型別是第二種型別,而鍵型別是第一種型別。

更新於:2022年12月13日

2K+ 次瀏覽

開啟你的職業生涯

完成課程獲得認證

開始學習
廣告