首頁  >  文章  >  後端開發  >  C++程式會依值對字典進行排序

C++程式會依值對字典進行排序

WBOY
WBOY轉載
2023-09-06 22:49:06917瀏覽

C++程式會依值對字典進行排序

有一些被稱為字典的資料結構在各種電腦語言中可用。一種特殊形式的更快的數據結構,它根據鍵和值存儲數據,就是字典。它將鍵值對保留在那裡,以便可以透過鍵快速搜尋某些組件,幾乎即時。類似字典的資料結構包含在C STL語言標準中。這個資料結構被稱為"map"。 map產生任何類型的鍵和值對(類型必須在編譯之前定義,因為我們使用的是C )。在本節中,我們將看看如何使用C 根據其值對字典條目進行排序。

我們先來看看地圖資料結構是如何定義的。這些內部模板需要兩種。所需的函式庫和語法顯示如下 -

定義地圖資料結構的語法

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

要在本例中使用地圖資料結構,我們必須匯入「map」函式庫。這需要類型 1 和 2 的資料。 Type1 表示鍵參數的資料類型,而 type2 表示值類型。從地圖類型類別派生的物件稱為mapVariable。現在讓我們研究一下如何根據這些關鍵因素來組織地圖。

使用Vector of Pairs

在這個想法中,我們只是創建了一個鍵值對的向量(動態數組,它是從C STL中獲得的另一個元素)。然後透過建立比較函數進行排序。然後將內容再次以排序的格式儲存到一個map中。

演算法

  • 以地圖 M 作為輸入

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

  • #對於 M 中的每個鍵值對 p,執行

    • #將 p 插入 A

  • #結束

  • 依照它們的鍵對A進行排序

  • 建立空地圖newMap

  • #對於 A 中的每對 p -

    • 將對 p 插入 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資料結構大部分時間以鍵的排序形式保存資料。在這裡,我們使用向量根據值進行排序。如果我們直接從向量中列印它們,可以找到順序。

使用一組對

可以使用另一種類型的資料結構-集合,對映射資料結構中的鍵值對進行排序。資料在集合資料結構中保持有序。因此,在集合中新增元素後,不需要再次進行排序。為了更好地理解,讓我們來看看演算法。

演算法

  • 以地圖 M 作為輸入

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

  • 對於 M 中的每個鍵值對 p,執行

    • #將 p 插入 S

  • #結束

  • 建立空地圖newMap

  • #對於 S 中的每一對 p -

    • 將對 p 插入 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是哈希映射,其鍵的資料使用哈希演算法進行儲存。儘管鍵是不同的,但是不同鍵的值可能相同。我們使用set和vector排序,其中向量和集合都攜帶了配對訊息,我們對它們進行了排序。每個配對都有兩種不同的排序方式。值類型是第二個類型,而鍵類型是第一個。

以上是C++程式會依值對字典進行排序的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:tutorialspoint.com。如有侵權,請聯絡admin@php.cn刪除