关联容器map, multimap

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的一个小例子:

#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;
}

 

运行结果:

 

下面是一个学生成绩录入和查询系统,应用了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://www.coursera.org/learn/cpp-chengxu-sheji

posted @ 2016-07-09 11:00  helloforworld  阅读(254)  评论(0编辑  收藏  举报