stl中的map和multimap
这个map其实就有点像python的字典的感觉。
后面是实现代码:
#include <iostream> #include <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; //注意这个地方给容器命名了。 int main() { 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容器中 mp.insert(make_pair(st.score, st.info)); //注意了往map容器中只能添加pair类型的,所以用make_pair来转化为map型 } else if (cmd[0] == 'Q') { int score; cin >> score; //lower_bound返回的是一个迭代器,意思是不超过score中最靠右边的元素 MAP_STD::iterator p = mp.lower_bound(score); //如果没有这个数的话,这个p会返回mp.begin(); 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; } } cout << maxp->second.name << " " << maxp->second.id << " " << maxp->first << endl; } else { cout << "Nobody" << endl; } } } return 0; }
注意点:
1,multimap容器中的元素是pair型的,所以在往map容器中添加元素的时候一定要记住用make_pair这个成员函数把两个数据组装成一个pair型的容器。访问是用first和second来进行访问的。
2,在使用map容器的时候,在前面要先进行定义也就是使用typedef这个关键字。给map命名。
3,要记住那些函数像lower_bound是返回相应的迭代器。
后面来继续看实例和map的内容:
#include <iostream> #include <map> #include <string> using namespace std; struct Student { string name; int score; }; Student student[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的分数为60 //假设我现在需要遍历这个容器 for (MP::iterator i = mp.begin(); i != mp.end(); i++) { cout << i->first << "," << i->second << endl; } Student st; st.name = 'Jack'; st.score = 99; pair<MP::iterator, bool> p = mp.insert(make_pair(st.name, st.score)); //这个pair容器第一个first是 if (p.second) { cout << p.first->first << p.first->second << endl; } else { cout << "insertion failed" << endl; } mp["Harry"] = 78; //插入一元素,其first的值为"Harry" MP::iterator q = mp.find("Harry"); cout << q->first << "," << q->second << endl; return 0; }
再来一题例题:
#include <iostream> #include <set> #include <map> #include <string> using namespace std; struct Word { //构造一个结构,这个结构中有出现次数和词语 int times; string wd; }; struct Rule { //这个其实和重载就很像,但是operator后面不是运算符,而是() bool operator ()(const Word& w1, const Word& w2)const { if (w1.times != w2.times) { return w1.times > w2.times; } else { return w1. wd < w2.wd; } } }; int main() { string s; set<Word, Rule> st; //这个set是用来按照规则排序的 map<string, int> mp; //这个mp是用来统计数据的,把统计完的数据放到set中进行排序 while (cin >> s) { ++mp[s];//这个中括号要多看几遍 //如果没有这个词就创建,如果有就++ } //后面来遍历 for (map<string, int>::iterator i = mp.begin(); i != mp.end(); i++) { //把map中的内容放到set容器里面去 Word tmp; tmp.wd = i->first; tmp.times = i->second; st.insert(tmp); } for (set<Word, Rule>::iterator i = st.begin(); i != st.end(); i++) { cout << i->wd << " " << i->times << endl; } return 0; }