The complexity of adding, deleting and searching data structures is limited to log(n), which is embodied in four sorting containers: multiset and Multimap in STL
Multiset header file:
Multimap header file:
This paper deals with several container traversal methods of multiset and Multimap: only with the help of iterators
multiset::iterator p;
p is an iterator, equivalent to a pointer, which can be used + +, –= As like as two peas, we can not compare size, we can not add or subtract integers, we can not subtract (unlike pointer, some containers are almost the same as pointer).
multiset
multisetst; It can sort automatically, and the start st is empty
Sorting rule: if the expression "a < b" is true, then a is in front of b (equivalent to the order from small to large)
Add element: st.insert()
Find element: st.find()
Delete element: st.erase()
multiset<int>st; int a[10] = { 2,6,7,3,9,5,6,8,3,9 }; for (int i = 0; i < 10; i++) { st.insert(a[i]); } multiset<int>::iterator i; //The container traverses the elements with the help of iterators, which is equivalent to pointers for (i = st.begin(); i != st.end(); i++) cout << *i << " "; cout << endl; //Output: 2 3 5 6 6 7 8 9 9 arranged in sequence i= st.find(2); //The return value of find is the iterator pointing to the element to be found. If the element does not exist, it points to end() if (i == st.end()) cout << "Not Found" << endl; else cout << *i << endl;
multiset can customize the collation
Multiset < int, greater > St ----------------- the sorting rule is from large to small
multiset<int ,Rule()>st;----------- Custom collation
find(x): find a Y in the sorting container, so that "x must be in Y" and "y must be in front of X" are not tenable. It is not a general "= =". It should be analyzed according to the sorting rules
set
The difference between set and multiset is that there can be no duplicate elements in the container
A and b repeat: "a must be in front of b" and "b must be in front of a" are not tenable
set may not insert elements successfully (elements already in the container cannot be inserted)
Usage of set:
set<int>my_set; int a[6]={4,7,4,9,7,6}; for(int i=0;i<6;i++) my_set.insert(a[i]); set<int>::iterator i; for(i=my_set.begin();i!=my_set.end();i++) cout<<*i<<" "; //Output 4 6 7 9 no duplicate elements cout<<endl; //Since the set is likely to be inserted unsuccessfully, you need to judge after inserting the element pair<set<int>::iterator,bool>res=my_set.insert(8); /* pair This data type is equivalent to struct pair { set<int>::iterator first; //Returns an iterator that points to the location of the inserted element bool second; //Returns whether the insertion was successful It contains two data members, namely, two variables T1 and T2 with pair < T1 and T2 >; } */ if(!res.second) cout<<*res.first<<"already exists"<<endl; else cout<<*res.first<<"inserted"<<endl;
multimap
The elements in the multimap container are in the form of pair,
multimap<T1,T2>mp;
struct
{
T1 first; // Keywords (key)
T2 second; // value
};
The elements in mulrimap are sorted by first and can be searched by first. The default sorting rule is "a.first < b.first". If "a.first" is true, a will be in front of B
It is suitable for programs that need to frequently add and delete elements and find elements. The elements are always orderly, and the efficiency of using multimap is high
Case: student score entry, including student name, student number, student score, student number is unique, and both name and score are not unique. The program has two types of input, namely, the addition information "Add name id score" and the query information "Query score" appear alternately. When querying information, it is to query the information corresponding to the student whose maximum score is less than the input score, If there are multiple qualified information outputs, the one with the largest student number
Because the program needs to constantly add information and query information, in order to be more efficient when the data is larger and query with score as the keyword, the multimap data structure is selected
#include<bits/stdc++.h> #Include < Map > / / header files to be included in Multimap and map using namespace std; struct Info //Define student information structure { string name; int id; }; struct Stu //Define score (keyword) -- information (value) structure, which is equivalent to multimap type { int score; Info info; }; typedef multimap<int,Info> MAP; int main() { MAP mp; Stu stu; char cmd[20]; while(cin>>cmd) { if(cmd[0]=='A') { cin>>stu.info.name>>stu.info.id>>stu.score; mp.insert(make_pair(stu.score,stu.info)); //make_pair generates a pair < int, info > variable //Its first equals st.score and second equals st.info } else if(cmd[0]=='Q') { int score; cin>>score; MAP::iterator p=mp.lower_bound(score); //Use lower_bound to find the student information corresponding to the maximum score lower than the score //Students who indicate [bgein(),p) have lower scores than score, but what p points to will not be lower than score (greater than or equal to score) if(p!=mp.begin()) //Indicates that a qualified student has been found { p--; //Start from the first student who meets the conditions int sco=p->first; int max=p->second.id; MAP::iterator t_mp=p; while((p!=mp.begin())&&(p->first==sco)) //Traverse all students with the same score { if(p->second.id>max) { t_mp=p; max=p->second.id; } p--; } cout<<t_mp->second.name<<" "<<t_mp->second.id<<" "<<t_mp->first; } //T cannot be written directly_ MP - > info (if this data member does not exist, write t_mp - > Second); else cout<<"not found"<<endl; //Indicates that there are no students with a lower score } } return 0; }
map
There can be no elements with duplicate keywords. Duplicate elements mean that the two elements can be the first and the last
You can use [], the subscript is the keyword, and the return value is second of the element with the same keyword as first (check the value through the keyword)
Inserting the element may fail because the keyword may already exist