c++学习笔记(四)

STL (Standard Template Library) 标准模版库


笔记三


  • pair
pair a;      //T1 T2指的是类型
等价于
  struct 
  {
    T1 first;
    T2 second;
  }

3. multimap

  • 要#include
  • 容器里都是pair形式的 multimap

  • pair的first称为关键字,second称为值
  • 排序和查找都是根据first,排序规则是 表达式a.first < b.first为真时,a排前面

  • 应用: 要完成以下功能

c++学习笔记(四)_第1张图片

#include
#include 
#include 
#include 
using namespace std;

struct StudentInfo
{
    int id;
    char name[20];
};
struct Student
{
    int score;
    StudentInfo info;
};
typedef multimap MP;

int main()
{
    MP mp;
    string cmd;
    Student st;
    while(cin >> cmd)
    {
        if(cmd == "Add")
        {
            cin >> st.info.name >> st.info.id >> st.score;
            mp.insert(make_pair(st.score,st.info)); // make_pair(T1,T2)产生一个pair
            //mp根据score排序,从小到大
        }
        else if(cmd == "Query")
        {
            int score;
            cin >> score;
            MP::iterator p = mp.lower_bound(score); //找到则返回指向大于等于score的元素的迭代器,否则返回mp.begin()
            if(p != mp.begin())
            {
                 p--;
                 score = p->first;
                 int maxId = p->second.id;
                 MP::iterator maxP = p;
                 for(;p != mp.begin() && p->first == score;p--)//可能有多个符合,在分数相同的人中找id最大的
                 {
                     if(p->second.id > maxId)
                     {
                         maxP = p;
                         maxId = p->second.id;
                     }
                 }
                 if(p->first == score) //如果循环是因为 p = mp.begin()时终止的,说明从 mp.begin()到一开始的maxP 的分数都相同,在循环时还没比较mp.begin(),所以要对mp.begin()另外作比较
                 {
                     if(p->second.id > maxId)
                     {
                         maxP = p;
                     }
                 }
                 cout << maxP->second.name << " " << maxP->second.id << " "<< maxP->first<

4. map

  • 与multimap区别
    • 不可以有关键字重复,插入元素可能失败
    • 可以使用下标,下标为关键字first,返回值是first对应的second
    • 如果使用下标时,该关键字并不存在,则会插入以该关键字作为first的元素
struct Student
{
    char name[20];
    int score;
};
typedef map MP;

int main()
{
    Student s[6] ={ {"Alier",78},{"Mestry",66},{"Jane",87},
                    {"Hebe",93},{"Arre",87} };
    MP mp;
    for(int i = 0;i < 5;i++)
    {
        mp.insert(make_pair(s[i].name,s[i].score));
    }
    cout << mp["Jane"] << endl; //输出87
    mp["Jane"] = 66; //只是second改变了,原数组的值并没变
    pair p = mp.insert(make_pair("Jane",44));
    if(p.second) //输出 insertion failed
    {
        cout << p.first->first << " " << p.first->second << " inserted" << endl;
    }
    else
    {
        cout << "insertion failed" << endl;
    }
    mp["Jack"] = 88; //本来不存在,mp将插入新元素,first为"Jack",second为88
    return 0;
}
  • 与set结合
    • 要实现以下功能

c++学习笔记(四)_第2张图片

struct Word
{
    string name;
    int times;
};
struct Rule
{
    bool operator()(const Word &a1,const Word &a2)
    {
        if(a1.times != a2.times)
            return a1.times < a2.times;
        else
            return a1.name < a2.name;
    }
};

int main()
{
    map mp;
    set st;
    string str;
    while(cin >> str)
        mp[str]++;  //如果str不存在会新建并second初始化为0,然后++
    map::iterator p = mp.begin();
    for(p;p!= mp.end();p++)
    {
        st.insert({p->first,p->second});
    }
    set::iterator i = st.begin();
    for(i;i != st.end();i++)
    {
        cout << i->name << " " << i->times << endl;
    }
    return 0;
}

你可能感兴趣的:(c++学习笔记(四))