multimap map

multimap

#include< map >
multimap容器里的元素,都是pair类型的
multimap<T1,T2>mp;
struct {
T1 first; //关键字
T2 second; //值
};
multimap中的元素按照first排序,并可以按照first查找
缺省的排序规则是:a.first<b.first a在b前面(默认小->大)
插入:mp.insert(make_pair(,));
eg:学生成绩录入和查找系统
add name id score —添加一个学生信息
query score—查找 输出已有记录中分数比score低的最高分获得者的姓名学号分数 如果有多个学生满足条件 输出学号最大的,如果找不到满足条件的 输出nobody

#include<iostream>
#include<cstring>
#include<set>
#include<algorithm>
#include<map>
using namespace std;

struct studentinfo{
    int id;
    char name[20];
};
struct student{
    int score;
    studentinfo info;
};
typedef multimap<int,studentinfo>map_std;
//c c++ 都有————此后map_std 等价于multimap<int,studentinfo>

int main() {
    map_std mp; //按照first排序:默认从小到大
    student st; //score id name
    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));
        }
        //查询操作
        //输出已有记录中分数比score低的最高分获得者的姓名学号分数 如果有多个学生满足条件 输出学号最大的,如果找不到满足条件的 输出nobody
        else if(cmd[0]=='q'){
            int score;
            cin >> score;
            map_std::iterator p = mp.lower_bound(score);
            //p指向map_std[) 第一个小
            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){
                    if(p->second.id>maxid){
                        maxp = p;
                        maxid = p->second.id; }
                }
                if(p->first==score){
                    if(p->second.id > maxid){
                        maxp = p;
                        maxid = p->second.id;
                    }
                    cout << maxp->second.name << " "
                        << maxp->second.id << " "
                        << maxp->first << endl;
                }
            }
            //指向第一个:本身就是最小的
            else cout << "nobody" << endl;
        }
    }
    
    return 0;
}

map

map不能有关键字重复的元素,插入元素可能失败
可以使用[] 下标为关键字(一一对应),返回first和关键字相同的second

#include<iostream>
#include<cstring>
#include<set>
#include<algorithm>
#include<map>
using namespace std;

struct student{
    string name;
    int score;
};
student stus[5] = {{"Tom",12},{"lili",78},{"andy",71},{"cele",30},{"hehe",100}};
int main() {
    map<string,int>mp;
    for(int i=0; i<5; i++)mp.insert(make_pair(stus[i].name,stus[i].score));
    cout << mp["andy"] << endl;
    mp["andy"] = 0;
    student s;
    s.name = "kiki";
    s.score = 99;
    pair<map<string,int>::iterator,bool>p = mp.insert(make_pair(s.name,s.score));
    if(p.second) cout << "插入成功" << endl;
    else cout << "插入失败" << endl;
        map<string,int>::iterator i;
    for(i=mp.begin(); i!=mp.end(); i++) cout << i->first << i ->second << endl;
    return 0;
    /*
     71
     插入成功
     Tom12
     andy0
     cele30
     hehe100
     kiki99
     lili78
     */
}


    s.name = "Tom";
    s.score = 99;
    pair<map<string,int>::iterator,bool>p = mp.insert(make_pair(s.name,s.score));
    if(p.second) cout << "插入成功" << endl;
    else cout << "插入失败" << endl;
        map<string,int>::iterator i;
    for(i=mp.begin(); i!=mp.end(); i++) cout << i->first << i ->second << endl;
    return 0;
    /*
     插入失败
     Tom12
     andy71
     cele30
     hehe100
     lili78
     */
}

eg: 单词词频统计程序
字典序:

#include<iostream>
#include<cstring>
#include<set>
#include<algorithm>
#include<map>
using namespace std;

map<string,int>mp;
int main() {
    string s;
    int n;
    cin >> n;
    pair<map<string,int>::iterator,bool>p;
    while(n--){
        cin>>s;
        p = mp.insert(make_pair(s,1));
        if(!p.second) mp[s]++;
    }
    map<string,int>::iterator i;
    for(i=mp.begin();i!=mp.end();i++)
        cout << i->first << " "
        << i->second << endl;
    return 0;
}

发布了30 篇原创文章 · 获赞 0 · 访问量 689

猜你喜欢

转载自blog.csdn.net/jhckii/article/details/103604709