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