C++按值对字典进行排序的程序

有一些称为字典的数据结构,可用于各种计算机语言。基于键和值存储数据的一种特殊形式的更快数据结构是字典。它将键值对保留在那里,以便可以通过键几乎实时地快速搜索某些组件。类似字典的数据结构包含在C++ STL语言标准中。这种数据结构被称为“map”。映射生成一对任何类型的键和值(类型必须在编译之前定义,因为我们使用的是C++)。在本节中,我们将介绍如何使用 C++ 根据字典条目的值对字典条目进行排序。

我们先来看看地图数据结构是如何定义的。这些内部模板需要两种类型。所需的库和语法如下所示 -

定义地图数据结构的语法

<span style="color:#000000">#include <map>
map<type1, type2> mapVariable;
</span>

在这种情况下要使用地图数据结构,我们必须导入“map”库。这需要数据类型 1 和 2。Type1 表示键参数的数据类型,而 type2 表示值类型。从映射类型类派生的对象称为 mapVariable。现在让我们研究一下如何根据这些关键因素组织地图。

使用对向量

在这个想法中,我们只是创建一个键值对的向量(动态数组,它是STL的另一个元素C++。然后通过创建比较函数来执行排序。然后再次将内容以排序格式存储到地图中。

算法

  • 以地图 M 作为输入

  • 定义动态数组 A 以存储键值对

  • 对于 M 中的每个键值对 p,执行

    • 将 p 插入 A

  • 结束于

  • 根据键对 A 进行排序

  • 创建空地图新地图

  • 对于 A 中的每对 p −

    • 将 p 对插入新地图

  • 结束于

  • 返回新地图



#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 ) ; }

输出

<span style="color:#000000">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
</span>

我们已经执行了排序,如果我们将最终结果存储在map中,则在排序前后不会看到任何差异,这是因为map数据结构大部分时间都以其键的排序形式保存数据。在这里,我们使用向量根据值对它们进行排序。如果我们直接从矢量打印它们,则可以找到顺序。

使用一组对

映射数据结构中的键值对可以使用集合(另一种类型的数据结构)进行排序。数据按排序顺序保存在设置的数据结构中。因此,将元素添加到集合后,不再需要排序。为了更好地理解,让我们看一下算法。

算法

  • 以地图 M 作为输入

  • 定义一个集合 S 来存储键值对

  • 对于 M 中的每个键值对 p,执行

    • 将 p 插入 S

  • 结束于

  • 创建空地图新地图

  • 对于 S 中的每对 p −

    • 将 p 对插入新地图

  • 结束于

  • 返回新地图



#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 ) ; }

输出

<span style="color:#000000">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
</span>

结论

在这篇文章中,我们看到了两种不同的方法来对字典数据结构(在C++中称为map)进行排序并按值对其进行排序。由于映射是哈希映射,因此其密钥的数据使用哈希算法进行存储。尽管键是不同的,但单独键的值可能相同。使用集合和向量排序,其中向量和集合携带配对,我们对它们进行了排序。每对有两种不同的种类。值类型是第二种类型,而键类型是第一种类型。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值