C++STL standard library learning notes multimap

Posted by phast1 on Tue, 14 Dec 2021 13:09:52 +0100

preface:

In this note, I annotated most of the code. Some of my ideas and comments were marked in blue, and the key points and areas needing attention were marked in red.

In this article, we mainly introduce the usage and application of multimap

1.1 usage of ultimap

You need #include < Map > when using

The elements in the multimap container are in the form of pair, which is also a sorting container.

        mulitmap<T1,T2> mp;

Then the elements in mp are of the following types:

struct 
{
    T1 first;//keyword
    T2 second;//value
};

The elements in the multimap are sorted by first and can be found by first

multimap has the function of automatic sorting. Its bottom layer is implemented by balanced tree like set, and each node in multimap stores a pair of information, including a key and a value.

Each node in the set stores one information, only one key, but each key value is also unique. Set represents the concept of set.

The default collation is "a.first < b.first". If "a.first" is true, then a is ahead of B. That is, this is the default sort.

1.2 application of Multimap

A student achievement entry and query system accepts the following two inputs:

        Add name id score

        Query score

Name is a string of no more than 16 characters, with no space in the middle, representing the student's name. Id is an integer representing the student number. Score is an integer representing a score. The student number will not be repeated, and the scores and names may be repeated.

The two inputs alternate. The first input means to add a student's information. When encountering this input, write down the student's name, id and score. The second input means to query. When this input is encountered, the name, student number and score of the highest score winner with a score lower than score in the existing records will be output. If multiple students meet the conditions, the information of the student with the largest student number will be output. If no qualified students can be found, "nobody" is output

The procedure is as follows:

/*
A student achievement entry and query system accepts the following two inputs:
Add name id score
Query score
name Is a string of no more than 16 characters, with no spaces in the middle, representing the student's name. Id is an integer representing the student number. Score is an integer representing a score. The student number will not be repeated, and the scores and names may be repeated.
The two inputs alternate. The first input means to add a student's information. When encountering this input, write down the student's name, id and score.
The second input means to query. When this input is encountered, the name, student number and score of the highest score winner with a score lower than score in the existing records will be output.
If multiple students meet the conditions, the information of the student with the largest student number will be output. If no qualified students can be found, "nobody" is output
*/
/*
Input example:
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
 Output example:
Nobody
Jack 12 78
Percy 9 81
Tom 11 79
Tom 11 79
*/
#include<iostream>
#include<map>
#include<cstring>
using namespace std;
struct StudentInfo
{
    int id;
    char name[20];
};
struct Student
{
    int score;
    StudentInfo info;
};
typedef multimap<int,StudentInfo> MAP_STD;
//Here, int and studentinfo correspond to int and info in the structure
//After that, MAP_STD is equivalent to Multimap < int, studentinfo >
//typedef int* PINT;
//Then PINT is equivalent to int *. PINT p; Equivalent to int* p

int main(int argc, char const *argv[])
{
    MAP_STD mp;
    Student st;
    char cmd[20];
    while (cin >> cmd)
    {
        if (cmd[0] == 'A')
        {
            cin >> st.info.name >> st.info.id >> st.score;
            mp.insert(make_pair(st.score,st.info));
        }
        //make_pair generates a pair < int, studentinfo > variable
        //Its first equals st.score and second equals st.info
        else if (cmd[0] == 'Q')
        {
            int score;
            cin >> score;
            MAP_STD::iterator p = mp.lower_bound(score);//Keyword search
            if (p != mp.begin())//eureka
            {
                --p;
                score = p->first;//The highest score lower than the score to be queried
                MAP_STD::iterator maxp = p;
                int maxId = p->second.id;
                for (; p != mp.begin() && p->first == score; --p)
                {//Traverse all students whose grades are equal to the score and start comparing the differences of student numbers
                    if (p->second.id > maxId)
                    {
                        maxp = p;
                        maxId = p->second.id;
                    }
                    
                }
                if (p->first == score)
                {//If the above loop is because p = = MP If it terminates with begin (), the element pointed to by p needs to be processed
                    if (p->second.id > maxId)
                    {
                        maxp = p;
                        maxId = p->second.id;
                    }
                }
                cout<<maxp->second.name<<" "<<maxp->second.id<<" "<<maxp->first<<endl;
            }//lower_ The result of bound is begin, indicating that no one has a lower score than the query score
            else
            {
                cout<<"Nobody"<<endl;
            }
        }
        
    }
    
    return 0;
}

As always, emphasize the key points in the program:

1,make_pair(st.score,st.info)

        make_pair generates a pair < int, studentinfo > variable, which is equivalent to converting the structure into pair.

2,mp.insert(make_pair(st.score,st.info))

Inserted is pair type.

3,mp.lower_bound(score)

        lower_ The return value of bound is the iterator of a key (if the key does not exist, the iterator of the next key next to the key is returned). Here, the iterator with score greater than or equal is returned.

Postscript:

When taking notes in this section, I read many big men's blogs because of curiosity. Some contents are also supplemented by their blogs. After reading them, I feel that I am too good, and the gap is too big. I feel that I still have a long, long, long way to go. It's just that there are no small steps to thousands of miles. If they stop learning because they are too powerful, what's the meaning? They don't know whether they can do it without trying. Later, when I have learned it completely, I will sort out the complete information and blog.

Thank you for reading here. I hope you can find your happiness in the ocean of code. See you next blog.

Topics: C++ STL