4.3 Map and Multimap

Source: Internet
Author: User

Use map Multimap must contain header file map

*:multimap

1) Multimap definition

Template<class Key,class Pred=less<key>,class a=allocator<t>

Class Multimp

{

.....

typedef pair<const key,t>value_type; Value_type often use the

......

};

Multimap Each object is an object of the pair template class. The element first member variable is also called the keyword, and its type is key. The second member variable is also called a value, and the type is T. The elements in the Multimap container are sorted by keyword from small to large and allow multiple keywords to be the same.

The value_type in Multimap above actually represents the type of element in the container. C + + allows you to define types within a class.

2) member functions

3) Example

//Program 19.4.4.1 Student Information management programs implemented with Multimap#include <iostream>#include<map>//use Multimap to include this header file#include <string>using namespacestd;classcstudent{ Public:            structCinfo//Classes can also be defined inside a class        {            intID; stringname;        }; intscore; Cinfo info; //additional information for students};typedef Multimap<int,cstudent::cinfo>map_std;intMain () {MAP_STD MP;    Cstudent St; stringcmd;  while(Cin >>cmd) {if(cmd = ="ADD") {cin>> st.info.name >> st.info.id >>St.score;        Mp.insert (Map_std::value_type (st.score,st.info)); }        Else if(cmd = ="Query" ){            intscore; CIN>>score; Map_std::iterator P=Mp.lower_bound (score); if(p!=Mp.begin ()) {                 --p; Score= p->first;//than the highest score to query for low scoresMap_std::iterator MAXP =p; intMaxid = p->second.id;  for(; P! = Mp.begin () && p->first = = score;--p) {//Traverse all grades and score equal students                    if(P->second.id >Maxid) {MAXP=p; Maxid= p->second.id; }                }                if(P->first = = score) {//If the above loop is because p = = Mp.begin ()//and terminated, the element that P points to is also processed                    if(P->second.id >Maxid) {MAXP=p; Maxid= p->second.id; }} cout<< Maxp->second.name <<" "<< maxp->second.id <<" "<< Maxp->first <<Endl; }            Else //The result of the lower_bound is the begin, stating that no one scores lower than the query scorecout <<"Nobody"<<Endl; }    }    return 0;} 

**:map

1) definition

Template<class key,class t,class Pred=less<key>,class a=allocator<t>>

Class Map {

......

typedef pair<const key,t>value_type;

.......

};

Map and Multimap are very similar, except that the elements in the map container cannot be duplicated.

Multimap has a member function map, in addition to the map has member function operator[]:

T&operator[] (Key k);

This member function returns the second portion of an element with a first value of K. If the first value of the container does not have an element that is equal to K, the element with the value K is automatically added, and if the member variable of the element is an object, it is initialized with the parameterless constructor.

2) Example

//example of the use of program 19.4.5.1.cpp map#include <iostream>#include<map>//use map to include this header fileusing namespacestd;template<classT1,classT2>ostream&operator<< (Ostream & O,ConstPair<t1,t2> &p) { //output the pair object to (First,second) Formo <<"("<< P.first <<","<< P.second <<")"; returno;} Template<classT>voidPrint (T first,t last) {//print interval [first,last]     for(; First! = last; + +)First ) cout<< * First <<" "; cout<<Endl;} typedef map<int,Double,greater<int> > MYMAP;//This container keyword is an integral type,//elements sorted by keyword from largest to smallintMain () {MYMAP MP; Mp.insert (Mymap::value_type ( the,2.7)); Pair<mymap::iterator,BOOL> P = Mp.insert (Make_pair ( the,99.3)); if( !p.second) cout<< * (P.first) <<"already exists"<< Endl;//will outputcout <<"1)"<< Mp.count ( the) << Endl;//Output 1) 1Mp.insert (Make_pair ( -,9.3)); cout<<"2)"<< mp[ +] << Endl;//If there is no element with a keyword of 40, insert acout <<"3)"; Print (Mp.begin (), Mp.end ());//output: 3) (40,0) (20,9.3) (15,2.7)mp[ the] =6.28;//change the element value of the keyword 15 to 6.28mp[ -] =3.14;//Insert the element with the keyword 17 and set its value to 3.14cout <<"4)";    Print (Mp.begin (), Mp.end ()); return 0;}

4.3 Map and Multimap

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.