STL map detailed usage

Map is associated with a container of the STL, which provides one (which can be referred to as a first key, each keyword can appear only once in the map, the second value is called the key) a data processing ability.

Required libraries

#include <map>

Basic Operations

definition

map<string,int>m;

This is the definition of a string with a keyword to the value of the map int

insert

method 1:

map<string,int>m;
m["Bob"]=101;    
m["Alice"]=102;  
m["Eric"]=103;   

Method 2:

m.insert(pair<string,int>("Lee",104));

Method 3:

m.insert(map<string,int>::value_type("Karen",105)); 

Traversal

Defines a pointer iteration iter, to point map, the map of traversal.

#include<bits/stdc++.h>

using namespace std;

int main()
{
    map<string,int>m;
    m["Bob"]=101;
    m["Alice"]=102;
    m["Eric"]=103;
    map<string,int>::iterator iter;
    for(iter=m.begin(); iter!=m.end(); iter++)
        cout<<iter->first <<"->"<<iter->second<<endl;
}

The output is:

Alice->102
Bob->101
Eric->103

See map keywords automatically prevail in the interior, by lexicographic ordering, rather than sequentially input;

Note that when I experiment, I found such a phenomenon:

#include<bits/stdc++.h>

using namespace std;

int main()
{
    map<string,int>m;
    m["Bob"]=101;
    m["Alice"]=102;
    m["Eric"]=103;
    map<string,int>::iterator iter;
    for(iter=m.begin(); iter!=m.end(); iter++)
        cout<<iter->first <<"->"<<iter->second<<endl;
        
    if(m["AAA"]==0)
    cout<<"NO"<<endl;

    for(iter=m.begin(); iter!=m.end(); iter++)
        cout<<iter->first <<"->"<<iter->second<<endl;
        

}

When asked about the number of a map that does not exist when the returned value should be zero, but when you once again traverse, you will find the map has been more than a key-value pair, but the value is 0:

Alice->102
Bob->101
Eric->103
NO
AAA->0
Alice->102
Bob->101
Eric->103

Be sure to take good care of when do problems.

Seek

method 1:

cout<<m.find("Bob")->second<<endl;

If you search by keyword, search words will not garbled output

Method 2:

    map<string,int>::iterator iter1;
    iter1 = m.find(string("Bob"));
    if(iter1 != m.end())
    cout<<iter1->first <<"->"<<iter1->second<<endl;
    else    
    cout<<"no fount"<<endl;

Define a pointer to map, if not, returns m.end ()

delete

method 1

m.erase(iter1);

The same operation is pointer

Method 2

m.erase(string("AAA"));

Or delete keyword

 

map of the correlation function

  • begin () returns a pointer to the head of the map iterator
  • clear () removes all elements
  • count () returns the number of occurrence of the specified element
  • empty () if the map is empty Returns true
  • end () returns a pointer to the end of the map iterator
  • equal_range () return a special entry iterators
  • erase () removes an element
  • find () to find an element
  • get_allocator () Returns the map configurator
  • insert () insert elements
  • key_comp () function returns the key elements of the comparison
  • lower_bound () Returns the key-value> = a given element in a first position
  • MAX_SIZE () Returns the maximum number of elements that can be accommodated
  • rbegin () returns a pointer to the tail of the reverse map iterator
  • rend () returns a pointer to the head of the reverse map iterator
  • size () Returns the number of elements in the map
  • swap () exchanging two map
  • upper_bound, () Returns the key value> given element in a first position
  • value_comp () function returns the value of the comparison element

 

Guess you like

Origin www.cnblogs.com/dyhaohaoxuexi/p/11257335.html