关联容器map, multimap
2016-07-09 11:00
274 查看
map/multimap里放着的都是pair模板类的对象,且按first从小到大排序。
pair 模板:
template <class _T1, class _T2>
struct pair
{
typedef _T1 first_type;
typedef _T2 second_type;
_T1 first;
_T2 second;
pair():first(),second(){};
pair(const _T1 &__a, const _T2 & __b):first(__a),second(__b){}
template<class _U1, class _U2>
pair(const pair<_U1, _U2>& __p): first(__p.first),second(__p.second){} //拷贝构造函数
}
multimap中的元素由<关键字,值>组成,每个元素是一个pair对象,关键字就是first成员变量,其类型是key.
multimap中允许多个元素的关键字相同。元素按照first成员变量从小到大排列,缺省情况下用less<Key>定义关键字的“小于”关系。
下面是关于multimap的一个小例子:
运行结果:
![](https://images2015.cnblogs.com/blog/911704/201607/911704-20160709105902561-2102175923.png)
下面是一个学生成绩录入和查询系统,应用了multimap的例子,通过查找输出系统中比查询分数低的最大学生ID号的学生信息。
运行结果:
![](https://images2015.cnblogs.com/blog/911704/201607/911704-20160709105813546-1571648199.png)
参考链接:
https://www.coursera.org/learn/cpp-chengxu-sheji
pair 模板:
template <class _T1, class _T2>
struct pair
{
typedef _T1 first_type;
typedef _T2 second_type;
_T1 first;
_T2 second;
pair():first(),second(){};
pair(const _T1 &__a, const _T2 & __b):first(__a),second(__b){}
template<class _U1, class _U2>
pair(const pair<_U1, _U2>& __p): first(__p.first),second(__p.second){} //拷贝构造函数
}
multimap中的元素由<关键字,值>组成,每个元素是一个pair对象,关键字就是first成员变量,其类型是key.
multimap中允许多个元素的关键字相同。元素按照first成员变量从小到大排列,缺省情况下用less<Key>定义关键字的“小于”关系。
下面是关于multimap的一个小例子:
#include <iostream> #include <map> using namespace std; int main() { typedef multimap<int, double, less<int>> mmid; mmid pairs; cout << "1)" << pairs.count(15) << endl; //map中定义typedef value_type pair<const key,T>,模板类为pair<int,double> pairs.insert(mmid::value_type(2, 3.4)); cout << "2)" << pairs.count(2) << endl; pairs.insert(mmid::value_type(1, 2.4)); for (mmid::iterator it = pairs.begin(); it != pairs.end(); it++) { cout << "(" << (*it).first << "," << (*it).second << ")" << " "; } cout << endl; return 0; }
运行结果:
![](https://images2015.cnblogs.com/blog/911704/201607/911704-20160709105902561-2102175923.png)
下面是一个学生成绩录入和查询系统,应用了multimap的例子,通过查找输出系统中比查询分数低的最大学生ID号的学生信息。
#include <iostream> #include <map> #include <string> using namespace std; class Student { public : int score; struct Info { int ID; string name; }; Info info; }; typedef multimap<int, Student::Info> MAP; int main() { MAP map; Student student; string cmd; while (cin >> cmd) { if (cmd == "add") { cin >> student.score >> student.info.ID >> student.info.name; map.insert(MAP::value_type(student.score, student.info)); } else if (cmd == "query") { int sco = 0; cin >> sco; MAP::iterator it; it = map.lower_bound(sco); if (it != map.begin()) { it--; sco = it->first; int maxid = (*it).second.ID; MAP::iterator maxp = it; for (; it != map.begin() && (*it).first == sco; it--) { if ((*it).second.ID > maxid) { maxid = (*it).second.ID; maxp = it; } } if (it == map.begin()) { if (maxid < it->second.ID) { maxid = it->second.ID; maxp = it; } } cout << maxp->first << " " << maxp->second.ID << " " << maxp->second.name << endl; } else { cout << "Not find !" << endl; } } } return 0; }
运行结果:
![](https://images2015.cnblogs.com/blog/911704/201607/911704-20160709105813546-1571648199.png)
参考链接:
https://www.coursera.org/learn/cpp-chengxu-sheji
相关文章推荐
- ViewPager+Fragment懒加载
- 单链表查找倒数第k个元素
- Android学习笔记
- Linux C第一课
- window平台安装MongoDB
- Cesium 概述 (一)
- The dependency `XXXX` is not used in any concrete target.
- 微信语音连播的实现思路
- java基础知识回顾---String、StringBuffer与StringBuilder之间区别
- 线性时间内筛素数+欧拉函数+莫比乌斯函数
- 分布式技术一周技术动态 2016.07.10
- android之postDelayed是否运行在主线程中解答
- 查询出来的内容只显示一部分,后边用的内容进行替换
- 18个高大上的浏览器小技巧
- mysql用户管理
- python学习——数据类型和变量
- Android Volley框架使用(二)
- 关联容器set, multiset
- 有关于__align(n) ,内存对齐
- 游戏制作之路:游戏引擎选择、Mac下和Windows下UnrealEngine 4体验对比、文档及其他 -- 前几天我说要学做游戏的流程...