c ++ related to the map

Map is an associative container STL, which provides one to one (the first of which can be called key (key), each keyword may only appear once in the map, the second might be called the value of the keyword (value)) of the data processing capacity, because of this characteristic, it is possible to complete the one-time processing of data we provide on the fast-track program. Here that organization, internal map next map data inside a self-built red-black tree (balanced binary tree on one kind of non-strict sense), The tree has a function to automatically sort the data, so all data are internal map orderly .

1, map Introduction

map is a type of associative containers. It is characterized by the impact of the addition and deletion of nodes iterator is very small, in addition to the operation node, have no effect on other nodes.

Iterators, the real value can be modified, but not modified key.

2, map functions

value corresponds - Key automatically established. key and value can be any type you need.

Quick Find records based on the key value, find the complexity is substantially the Log (N), if there are 1000 records, to find a maximum of 10 times, 1,000,000 records, to find a maximum of 20 times.

Key -Value quickly insert records.

Quick delete records

The record modified value Key.

Through all the records.

3, using the map

Use the map to obtain include the header file where the class map

#include <map> // STL header files with no extension .h

map object is a template class, you need to store keywords and objects two template parameters:

std:map<int,string> personnel;

This defines an int pointer as an index, and has an associated string of points.

4, map constructor

Providing a total of six map constructor, this relates to the memory allocator stuff, not Skip Table in the following we will come into contact with some of the map constructor, in here to say is that we usually constructed by a method as follows map:

map<int, string> mapStudent;

5, insertion of data

After the container structure map, we can fill insert the data. The method of inserting data mentioned here two:

We came to know through the second pair of variables is inserted success, its first variable returns a map iterator, if you insert successful Insert_Pair.second should be true, and otherwise false.

 The following can be seen when the code for a method for inserting, when the key corresponding to the value already exists can not be re-key corresponding to the value '

#include <the iostream> 
#include <the cmath> 
#include <algorithm> 
#include < SET > 
#include <cstdio> 
#include <Map> 
#include < String > 
#include <CString>
 / * @author: bk 
* Family: 
* Time: 
* / 
// I'm like a child playing at the beach,
 // time to time to pick up more than the usual smooth stones or more beautiful shells rejoice,
 // and demonstrated in front of me is totally unproved sea truth 
the using  namespace STD;
 int main () 

{ 

    Map < int , String > mapStudent;

    pair<map<int, string>::iterator, bool> Insert_Pair;

    Insert_Pair = mapStudent.insert(pair<int, string>(1, "student_one"));

    if(Insert_Pair.second == true)

        cout<<"Insert Successfully"<<endl;

    else

        cout<<"Insert Failure"<<endl;

    Insert_Pair = mapStudent.insert(pair<int, string>(1, "student_two"));

    if(Insert_Pair.second == true)

        cout<<"Insert Successfully"<<endl;

    else

        cout<<"Insert Failure"<<endl;

    map<int, string>::iterator iter;

    for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)

       cout<<iter->first<<' '<<iter->second<<endl;

}

The following code shows the corresponding key value can be overridden by a second method

// verify the effect of inserting data array form   
  
#include <Map>   
  
#include < String >   
  
#include <the iostream>   the using namespace STD;   int main ()   
{   
    Map < int , String > mapStudent;   
    mapStudent [ . 1 ] = " student_one " ;   
    mapStudent [ . 1 ] = " student_two " ;   
    mapStudent [ 2 ] = " student_three " ;   
    Map
  
 
  

  
  
  
  
  
  <int, string>::iterator iter;  
  
    for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)  
  
       cout<<iter->first<<' '<<iter->second<<endl;  
} 

5, finding data (by key lookup)

Here are two data lookup method:

The first: with a count function to determine if the keyword appears, the drawback is unable to locate the position of the data appear, due to the characteristics of the map, one to one mapping relationship, determines the return value of the function of only two count, either 0 either 1, happens, of course, is a return 1

The second: use the find function to locate the position data is present, it returns an iterator, when data is present, it returns an iterator data is located, if the map does not want to find data, it returns an iterator equal to end function returns an iterator.

#include <map>  
  
#include <string>  
  
#include <iostream>  
  
using namespace std;  
  
int main()  
  
{  
  
    map<int, string> mapStudent;  
  
    mapStudent.insert(pair<int, string>(1, "student_one"));  
  
    mapStudent.insert(pair<int, string>(2, "student_two"));  
  
    mapStudent.insert (pair < int , String > ( . 3 , " student_three " ));   
  
    Map < int , String > :: Iterator ITER;   
  
    ITER = mapStudent.find ( . 1 );   
  
    IF (! = mapStudent.end ITER ()) // return the last iteration of the data when the key is not in the presence of a 
  
       cout << " the Find, at the value iS " << iter-> SECOND, << endl;   
  
    the else   
  
       cout << " the Do not the Find " << endl;  
      
    return 0;  
}  

iterator type of data acquired by the map object is a method std :: pair objects, comprising iterator-> first and iterator-> second key representing the stored data and the two data.

 6, remove elements from the map

To remove a map with an entry in the erase ()

The member method is defined as follows:

iterator erase (iterator it); // delete an entry by subject

iterator erase (iterator first, iterator last) // delete a range

size_type erase (const Key & key); // delete keyword

clear () equivalent enumMap.erase (enumMap.begin (), enumMap.end ());

7, after sorting talk about it

 

Guess you like

Origin www.cnblogs.com/cstdio1/p/11041329.html