C++中的map排序

10 篇文章 6 订阅

目录

一、map对于key(键)的排序

(1)map中的key的默认排序

 (2)对于key的自定义排序

 (3)key是结构体的排序

二、map对于value(值)排序


一、map对于key(键)的排序

map中其实是有默认排序的,它里面的构造是用到红黑树,所以它的默认排序是按照键来排序的,并且是按照键的升序来排序的。

我们如果想要对这种排序进行自定义的话,可以通过自己写一个仿函数来解决,至于什么是仿函数,本篇文章不做解释,本篇文章只介绍怎么用(其实是我菜,哈哈哈)!

(1)map中的key的默认排序

#include<bits/stdc++.h>
using namespace std;
//对于map中的key的默认排序

map<int,string>m;

int main()
{
	m[1]="iui";
	m[87]="sjddd";
	m[2]="jsd";
	m[67]="yuuuu";
	for(auto it=m.begin();it!=m.end();it++){
		cout<<it->first<<" "<<it->second<<endl;
	}
	return 0;
}

 (2)对于key的自定义排序

#include<bits/stdc++.h>
using namespace std;
//对于map中的key进行paixu

struct rule{
	bool operator()(string a,string b){
		return a>b;//对于键是string型按照从大到小排
	}
};

int main()
{
	//map中的第三个参数其实就是排序规则,之前不写就会默认成默认排序
	map<string,int,rule>m;
	m["asas"]=199;
	m["zx"]=99;
	m["gsgus"]=878;
	m["yuy"]=1515;
	map<string,int,rule>::iterator it;
	for(it=m.begin();it!=m.end();it++){
		cout<<it->first<<" "<<it->second<<endl;
	}
	return 0;
}

 (3)key是结构体的排序

#include<bits/stdc++.h>
using namespace std;
//按照键是结构体的排序
typedef struct{
	string name;
	int score;
}node;
struct rule{
	bool operator()(node a,node b){
		if(a.score==b.score){
			return a.name>b.name;
		}
		return a.score>b.score;
	}
//排序规则是按照成绩大的在前面,相同按照名字降序
};

int main()
{
	node stu;
	map<node,int,rule>m;
	stu.name="abc";
	stu.score=88;
	m[stu]=1212;
	stu.name="acd";
	stu.score=88;
	m[stu]=1213;
	stu.name="bcbc";
	stu.score=100;
	m[stu]=1214;
	stu.name="zzzzzz";
	stu.score=1000;
	m[stu]=8989;
	map<node,int,rule>::iterator it;
	for(it=m.begin();it!=m.end();it++){
		cout<<"名字="<<it->first.name<<" 成绩="<<it->first.score<<" 学号="<<it->second<<endl;
	}
	return 0;
}

当然,还有第二种对于键是结构体的排序方法了

#include<bits/stdc++.h>
using namespace std;
//对于键是结构体的自定义排序
typedef struct node{
	int score;
	string name;
	bool operator <(const node &s)const{
		if(score!=s.score)return score>s.score;
        return name>s.name;
	}
}node;

int main()
{
	map<node,int>m;
	node u;
	u.name="abc";
	u.score=99;
	m[u]=12;
	u.name="bcd";
	u.score=99;
	m[u]=13;
	u.name="bbb";
	u.score=100;
	m[u]=14;
	u.name="yuy";
	u.score=15;
	m[u]=6666;
	map<node,int>::iterator it;
	for(it=m.begin();it!=m.end();it++){
		cout<<"成绩="<<it->first.score<<" 名字="<<it->first.name<<" 学号="<<it->second<<endl;
	}
	return 0;
}

二、map对于value(值)排序

在map中的排序是基于按照key来排序的,所以无法对value直接进行排序,如果想对value进行排序,需要用到vector容器以及sort函数,当然了还有自定义的排序规则(就叫仿函数拉倒),其实也是一套模板而已

#include<bits/stdc++.h>
using namespace std;
//map中对于value排序
//之前说的map是个键值对,所以需要vector
//来接收的话,那么就需要一对一,就需要用到pair了

bool cmp(const pair<string,int> a,pair<string,int>b){
	return a.second>b.second;
}

int main()
{
	map<string,int>m;
	m["asas"]=18;
	m["ioio"]=90;
	m["cj"]=89;
	vector<pair<string,int>>v(m.begin(),m.end());
	sort(v.begin(),v.end(),cmp);
	map<string,int>::iterator it;
	for(int i=0;i<v.size();i++){
		cout<<v[i].first<<" "<<v[i].second<<endl;
	}
	return 0;
}

  • 47
    点赞
  • 218
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论
在 C 语言,没有直接的内建数据结构来实现类似于 C++ 的 `map`(关联数组)或 `std::map`(有序关联数组)的功能。不过,你可以通过使用自定义的数据结构和排序算法来实现类似的功能。 一种常见的方法是使用数组或链表来实现关联数组,然后对数组或链表进行排序。你可以使用标准库排序函数(例如 `qsort`)或自己实现排序算法,如冒泡排序或插入排序。 以下是一个使用数组和简单的冒泡排序来实现关联数组排序的示例代码: ```c #include <stdio.h> #include <string.h> typedef struct { int key; int value; } KeyValuePair; void bubbleSort(KeyValuePair arr[], int n) { for (int i = 0; i < n - 1; i++) { for (int j = 0; j < n - i - 1; j++) { if (arr[j].key > arr[j + 1].key) { KeyValuePair temp = arr[j]; arr[j] = arr[j + 1]; arr[j + 1] = temp; } } } } int main() { KeyValuePair data[] = { {3, 30}, {1, 10}, {2, 20} }; int n = sizeof(data) / sizeof(data[0]); bubbleSort(data, n); printf("Sorted array:\n"); for (int i = 0; i < n; i++) { printf("Key: %d, Value: %d\n", data[i].key, data[i].value); } return 0; } ``` 这个示例使用 `KeyValuePair` 结构体来表示关联数组的元素,其 `key` 表示键,`value` 表示值。通过冒泡排序算法对 `data` 数组的元素按照键进行排序,并输出排序后的结果。 请注意,这只是一个简单的示例,实际使用时你可能需要根据自己的需求进行适当的修改和扩展。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

菜到极致就是渣

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值