4.3 map和multimap

2023-05-09,,

使用map multimap必须包含头文件map

*:multimap

1)multimap定义

template<class Key,class Pred=less<Key>,class A=allocator<T>

class multimp

{

.....

typedef pair<const Key,T>value_type;    //value_type经常用到

......

};

multimap每个对象都是pair模板类的对象。元素first成员变量也称关键字,其类型为Key。second成员变量也称值,类型为T。multimap容器里的元素是按照关键字从小到大排序的,且允许多个关键字相同。

上面multimap中的value_type实际上表示容器中的元素的类型。c++允许在类的内部定义类型。

2)成员函数

3)例

//program 19.4.4.1 用multimap实现的学生信息管理程序
#include <iostream>
#include <map> //使用multimap需要包含此头文件
#include <string>
using namespace std;
class CStudent
{
public:
struct CInfo //类的内部还可以定义类
{
int id;
string name;
};
int score;
CInfo info; //学生的其他信息
};
typedef multimap<int,CStudent::CInfo> MAP_STD;
int main()
{ MAP_STD mp;
CStudent st;
string cmd;
while( cin >> cmd ) {
if( cmd == "Add") {
cin >> st.info.name >> st.info.id >> st.score ;
mp.insert(MAP_STD::value_type(st.score,st.info ));
}
else if( cmd == "Query" ){
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;
}
else //lower_bound的结果就是 begin,说明没人分数比查询分数低
cout << "Nobody" << endl;
}
}
return ;
}

**:map

1)定义

template<class Key,class T,class Pred=less<Key>,class A=allocator<T>>

class map {

......

typedef pair<const Key,T>value_type;

.......

};

map 和multimap十分相似,区别在于,map容器中的元素,关键字不能重复。

multimap有的成员函数map都有,此外map还有成员函数operator[]:

T&operator[](Key k);

该成员函数返回first值为k的元素的second部分。如果容器中没有元素的first值等于k,则自动添加值为k的元素;如果该元素的成员变量是一个对象,则用无参构造函数对其初始化。

2)例

//program 19.4.5.1.cpp  map的用法示例
#include <iostream>
#include <map> //使用map需要包含此头文件
using namespace std;
template <class T1,class T2>
ostream & operator <<( ostream & o,const pair<T1,T2> & p)
{ //将pair对象输出为 (first,second)形式
o << "(" << p.first << "," << p.second << ")";
return o;
}
template<class T>
void Print( T first,T last)
{//打印区间[first,last)
for( ; first != last; ++ first)
cout << * first << " ";
cout << endl;
}
typedef map<int,double,greater<int> > MYMAP; //此容器关键字是整型,
//元素按关键字从大到小排序
int main()
{
MYMAP mp;
mp.insert(MYMAP::value_type(,2.7));
pair<MYMAP::iterator,bool> p = mp.insert(make_pair(,99.3));
if( ! p.second )
cout << * (p.first) << " already exists" << endl; //会输出
cout << "1) " << mp.count() << endl; //输出 1) 1
mp.insert(make_pair(,9.3));
cout << "2) " << mp[] << endl;//如果没有关键字为40的元素,则插入一个
cout << "3) ";Print(mp.begin(),mp.end());//输出:3) (40,0)(20,9.3)(15,2.7)
mp[] = 6.28; //把关键字为15的元素值改成6.28
mp[] = 3.14; //插入关键字为17的元素,并将其值设为3.14
cout << "4) ";Print(mp.begin(),mp.end());
return ;
}

4.3 map和multimap的相关教程结束。

《4.3 map和multimap.doc》

下载本文的Word格式文档,以方便收藏与打印。