C++程式:按鍵排序字典
在不同的程式語言中,都有一些被稱為字典的資料結構。字典是一種特殊的、更快的儲存資料結構,它基於鍵值對儲存資料。它將鍵值對儲存起來,以便可以透過鍵在幾乎恆定的時間內輕鬆搜尋某些元素。在 C++ 中,字典式資料結構存在於 C++ STL 中。這種資料結構的名稱為“map”。map 建立了任意型別的鍵值對(因為我們使用的是 C++,所以必須在編譯前定義型別)。在本節中,我們將瞭解如何在 C++ 中根據鍵引數對字典的條目進行排序。
讓我們先看看如何定義 map 資料結構。這將需要兩種型別的模板。語法和所需的庫如下所示:
定義 Map 資料結構的語法
#include <map> map<type1, type2> mapVariable;
在這裡,我們需要匯入“map”庫來使用 map 資料結構。它接受兩種資料型別 type1 和 type2。Type1 指的是鍵引數的資料型別,而 type2 指的是值型別。mapVariable 是從 map 型別類建立的物件。現在讓我們看看如何使用它們的鍵引數對 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.first < b.first;
}
//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 ) {
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 },
{ "One", 1 }
};
cout << "Before Sorting: " << endl;
display( givenMap );
cout << "After Sorting: " << endl;
givenMap = sorting( givenMap );
display( givenMap );
}
輸出
Before Sorting: Key: One, value: 1 Key: Three, value: 3 Key: Two, value: 2 After Sorting: Key: One, value: 1 Key: Three, value: 3 Key: Two, value: 2
我們已經執行了排序,但這裡我們看不到任何區別。這是因為 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 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 ) {
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 },
{ "One", 1 },
{"Four", 4},
{"Five", 5},
};
cout << "Before Sorting: " << endl;
display( givenMap );
cout << "After Sorting: " << endl;
givenMap = sorting( givenMap );
display( 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: Five, value: 5 Key: Four, value: 4 Key: One, value: 1 Key: Three, value: 3 Key: Two, value: 2
結論
在本文中,我們看到了兩種不同的技術來對字典資料結構(C++ 中的 map)進行排序,並根據它們的鍵引數進行排序。map 是雜湊對映,它們使用雜湊技術來儲存鍵對應的值。鍵是唯一的,而不同的鍵可能具有相同的值。我們使用集合和向量的排序來對它們進行排序,其中向量和集合儲存鍵值對。每個鍵值對包含兩種型別。第一種型別是鍵型別,第二種型別是值型別。
資料結構
網路
關係型資料庫管理系統 (RDBMS)
作業系統
Java
iOS
HTML
CSS
Android
Python
C語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP