《程序设计实习》之【STL-map和multimap】

预备知识: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) { }
};

map/multimap里放着的都是pair模版类的对象,且按first从小到大排序第三个构造函数用法示例:

pair<int,int>
p(pair<double,double>(5.5,4.6));
// p.first = 5, p.second = 4

muiltmap

template<class Key, class T, class Pred = less<Key>,
class A = allocator<T> >
class multimap {
    …
    typedef pair<const Key, T> value_type;
    ……
};
//Key 代表关键字的类型
  • multimap中的元素由 <关键字,值> 组成,每个元素是一个pair对象,关键字 就是first成员变量,其类型是Key
  • multimap 中允许多个元素的关键字相同。元素按照first成员变量从小到大排列,缺省情况下用 less 定义关键字的“小于”关系。

muiltmap示例

#include <iostream>
#include <map>
using namespace std;
int main() {
    multimap<int, double> pairs;
    cout << "1) " << pairs.count(15) << endl;
    pairs.insert(make_pair(15, 2.7)); 
    pairs.insert(make_pair(15, 99.3));
    cout << "2) " << pairs.count(15) << endl; //求关键字等于某值的元素个数
    pairs.insert(make_pair(30, 111.11));
    pairs.insert(make_pair(10, 22.22));
    pairs.insert(make_pair(25, 33.333));
    pairs.insert(make_pair(20, 9.3));
    multimap<int, double>::const_iterator i = pairs.begin();
    for( ; i != pairs.end() ; i++ )
    cout << "(" << i->first << "," << i->second << ")" << ",";
}

输出:

1)0
2)2
(10,22.22),(15,2.7),(15,99.3),(20,9.3),(25,33.333),(30,111.11)

multimap例题

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

输入样例:
Add Jack 12 78
Query 78
Query 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

map

template<class Key, class T, class Pred = less<Key>,
class A = allocator<T> >
class map {
    …
    typedef pair<const Key, T> value_type;
    ……
};

map 中的元素都是pair模板类对象。关键字(first成员变量)各不相同。元素按照关键字从小到大排列,缺省情况下用 less,即“<” 定义“小于”。

map的[ ]成员函数

若pairs为map模版类的对象,pairs[key]返回对关键字等于key的元素的值(second成员变量)的引用。若没有关键字为key的元素,则会往pairs里插入一个关键字为key的元素,其值用无参构造函数初始化,并返回其值的引用。
如:

map<int,double>

pairs;
pairs[50] = 5; 

会修改pairs中关键字为50的元素,使其值变成5。
若不存在关键字等于50的元素,则插入此元素,并使其值变为5。

map示例

#include <iostream>
#include <map>
using namespace std;
template <class Key, class Value>
ostream & operator <<( ostream & o, const pair<Key,Value> & p) {
    o << "(" << p.first << "," << p.second << ")";
    return o;
}
int main() {
    map<int, double, less<int> > pairs;
    cout << "1) " << pairs.count(15) << endl;
    pairs.insert(make_pair(15, 2.7));
    pairs.insert(make_pair(15, 99.3));   
    cout << "2) " << pairs.count(15) << endl;
    pairs.insert(make_pair(20, 9.3));
    map<int, double, less<int> >::iterator i;
    cout << "3) ";
    for( i = pairs.begin(); i != pairs.end(); i++ )
        cout << * i << ",";
    cout << endl;
    cout << "4) ";
    int n = pairs[40];   //如果没有关键字为40的元素,则插入一个
    for( i = pairs.begin(); i != pairs.end(); i++ )
        cout << * i << ",";
    cout << endl;
    cout << "5) ";
    pairs[15] = 6.28; //把关键字为15的元素值改成6.28
    for( i = pairs.begin(); i != pairs.end(); i++ )
        cout << * i << ",";
    return 0;
}
输出:
1) 0
2) 1
3) (15,2.7),(20,9.3),
4) (15,2.7),(20,9.3),(40,0),
5) (15,6.28),(20,9.3),(40,0),

猜你喜欢

转载自blog.csdn.net/beashaper_/article/details/80711446
今日推荐