STL_multimap和STL_map--算法--笔记

multimap容器里的元素,都是pair形式的
multimap<T1,T2>map;

则map里的元素都是如下类型:

struct{
  T1 first;//关键字
  T2 Second;//值
 };

和java的map差不多都是
k-V对,只是方法形式不同。
multimap中的元素按照first排序,并可以按first进行查找
缺省的排序规则是"a.first<b.first"为true,则a排在b前面

multimap的应用
一个学生成绩录入和查询系统。

name是一个不超过16字符的字符串,中间没有空格,代表学生姓名
id是个整数输出,代表学号。
score是个整数,表示分数,学号不会重复
分数和姓名都可能重复。
两种输入交替出现,
第一种输入表示要添加一个学生的信息,碰到这种输入,就记下学生的姓名,id和分数。
第二种输入表示要查询,碰到这种输入,就输出已有记录中分数比score低的最高分获得者的姓名,学号和分数。
如果多个学生都满足条件,就输出学好最大的那个
学生的信息。
如果找不到就输出“Nobody”

输入样例:

Add Jack 12 78
Qurey 78
Qurey 81
Add Percy 9 81
Add Marry 8 81
Query 82
Add Tom 11 79
Query 80
Query 81

输出样例

Nobody
Jack 12 78
Percy 9 81
Tom 11 79
Tom 11 79

#include<isotream>
#include<map>//使用multimap和map需要包含此头文件
#include<cstring>
using namespace std;
struct StudentInfo{
 int id;
 char name[20];
};
struct Student{
 int score;
 StudentInfo info;
};
typedef multimap<int,StudentInfo>MAP_STD;
//此后MAP_STD等价于multimap<int,StudentInfo>
//typedef int *PINT;
//此后PINT等价于int *。即PINT p;等价于 int *p;
int mian(){
 MAP_STD mp;
 Student st;
 char cmd[20];
 while(cin>>cmd){
 if(cmd[0]=='A'){
 cin>>st.info.name>>st.info.id>>st.score;
 mp.insert(make_pair(st.score,st.info));
}//make_pair生成一个pair<int,StudnetInfo>变量
 //其first等于st.score,second等于st.info
else if(cmd[0]=='Q'){
int score;
cin>>score;
MAP_STD::iterator p=mp.lower_bound(score);
if(p!=mp.begin()){
--p;
score=p->first;//比要查询分数低的
MAP_STD::iterator maxp=p;
int maxId-p->second.id;
for(;p!=mp.begin()&&p->first==score;--p){
//遍历所有成绩和score相等的学生
if(p->second.id>maxId){
 maxp=p;
 maxId=p->second.id;
 }
}
 if(p->first==score){
 //如果上面循环因为p==mp.begin()二中制,则p指向的元素还要处理
 if(p->second.id>maxId){
  maxp=p;
  maxId=p->second.id;
  }
 }
 cout<<maxp->second.name<<" "
 <<maxp->second.id<<" "
 <<maxp->first<<endl;
 }
 //lower_bound的结果就是begin,说明没有人分数比查询分数底
  else cout<<"Nobody"<<endl;
   }
  }
  return ;0
 }

map的用法

和multimap区别在于:
不能有关键字重复的元素
可以使用[],下表为关键字,返回值first和关键字相同的元素的second
插入元素可能失败

#include<iostream>
#include<map>
#include<string>
using namespace std;
struct Student{
 string name;
 int score;
};
Student students[5]={
 {"Jack",89},{"Tom",74},{"Cindy",87},{"Alysa",87},{"Micheal",98}
};
typedef map<string,int >MP;
int main(){
MP mp;
for(int i=0;i<5;i++){
 mp.insert(make_pair(student[i].name),student[i].score));
 cout<<mp["Jack"]<<endl;//输出89
 mp["Jack"]=60;//修改名为Jack的元素的second为60
 for(MP::iterator i=mp.begin();i!=mp.end();++i)
  cout<<"("<<i->first<<","<<i->second<<")";
 // 输出:(Alysa,87)(Cindy,87)(Jack,60)(Micheal,98)(Tom,74)
  cout<<endl;
  Student st;
  st.name="Jack";
  st.score=99;
  pair<MP::iterator,bool>p=
   mp.insert(make_pair(st.name,st.score));
  if(p.second)
   cout<<"("<<p.first->first<<","<<p.first->second<<")inserted"<<endl;
  else 
   cout<<"insertion failed"<<endl;//输出此信息
  mp["Harry"]=78;//插入一元素,其first为“Harry”,然后将其second改为78
  MP::iterator q=mp.find("Harry");
  cout<<("<<q->first<<","<<q->second<<")"<<endl;
  //输出:(Harry,78)
  return 0;
}
}

set and map

单词词频统计程序

输入大量单词,每个单词,一行,不超过20字符,没有 空格。 按出现次数从多到少输出这些单词及其出现次数 。出现次数相同的,字典序靠前的在前面。

输入

this
is
ok
this
plus
that
is
plus
plus

输出

plus 3
is 2
this 2
ok 1
that 1

#include<iostream>
#include<cstdio>
#include<set>
#include<map>
#include<algorithm>
#include<cstring>
using namespace std;
struct word
{
    string wd;
    int time;
};
struct rule
{
    bool operator()(const word &w1,const word &w2)
    {
        if(w1.time!=w2.time)
            return w1.time>w2.time;
        else
            return w1.wd<w2.wd;
    }
};
int main()
{
    string s;
    map<string,int> mp;
    set<word,rule> st;
    map<string,int>::iterator p;
    set<word,rule>::iterator i;
    while(cin>>s)
    {
        mp[s]++;
    }
    for(p=mp.begin();p!=mp.end();p++)
    {
        word tmp;
        tmp.wd=p->first;
        tmp.time=p->second;
        st.insert(tmp);
    }
    for(i=st.begin();i!=st.end();i++)
        cout<<i->wd<<" "<<i->time<<endl;
    return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

NoteLoopy

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

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

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

打赏作者

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

抵扣说明:

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

余额充值