桶排与map

桶排

#include<stdio.h>

int d,n;//最好看看洛谷的对应题 d是票号 n是选举人

int main()

{

scanf("%d",&d);

a[d]++;


scanf("%d",&n);

for(int i=1;i<=n;i++)

{

while(a[i]--)

printf("%d ",i);

}

return 0;

}

对应例题 洛谷P1271 选举学生会

map

#include<iostream>
#include<map>
#include<string>
using namespace std;
int main()
{
map<string,int>m;
m["hello"]=2;
m["?"]=3;
m["e"]=0;
cout<<m["e"]<<endl;
for(anto it=m.begin();it!=m.end();it++)
{
cout<< m.rbegin() ->first <<' '<< m.rbegin() ->second<<endl;
cout<< m.begin() -> second <<' '<< m.begin() -> first<<endl;
}
cout<<m.size()<<endl;
return 0;
}

iterator 迭代器

map的自定义排序 与不同形式

#include<iostream>
#include<map>
#include<string>
using namespace std;
string rule{
bool operator ()(string a,string b)const{          //此处一定要修饰const
return a<b;

}
};
int main()
{
map<string,int,rule>m;//分别对应 (键 值 排序规则)
map<string,int,rule>::iterator it;  //不清楚为啥要再起一行
m["f"]=4;
m["a"]=6;
for(it =m.begin() ;it!= m.end();it++)
cout<< it -> first<<' '<< it -> second<<endl;
return 0;
}

输出

a 6
f 4

键为结构体

#include<iostream>
#include<map>
#include<string>
using namespace std;
typedef struct{
string name;
int score;
}node;
struct rule{
bool operator()(node a,node b)const{
if(a.score==b.sorce)
return a.name<b.name;
return a.sorce>b.sorce;
}
};
int main()
{
node stu;
node.name="adf";
node.score=88;
m[stu]=123;
node.name="ads";
node.score=88;
m[stu]=122;
map<node,int,rule>m;
map<node,int,rule>::iterator it;
for(it =m.begin();it!=m.end();it++)
cout<<it -> first<<' '<<it -> second<<endl;
return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值