C ++ C ++ usage in the map in map usage

Reprinted: C ++ in map usage

Characteristic map that all the elements are automatically sorted elements impaired. All elements of the map are the pair, also has real value (value) and key (key). The first element of the pair will be seen as the key, the second element will be treated as a real value. map does not permit two elements have the same key.

The following look at the definition of pair <stl_pair.h> of:

template <class T1, class T2>

struct pair{

  typedef T1 first_type;

  typedef T2 second_type;

  T1 first; // Note that it is public

  T2 second; // Note that it is public

  pair() : first(T1()),second(T2()) {}

  pair(const T1&a,const T2&b) :first(a),second(b) {}

};

When a client to map new elemental operations (insert) and delete (erase), all iterators before the operation, is still valid after the operation is completed. Except deleted iterators.

Standard STL map is a red-black tree as the underlying mechanism is completed, the contents of each node is a pair.

A, map constructor substantially

map<string , int >strMap;        

map<int ,string >intMap;

map<sring, char>strMap;        

map< char ,string>charMap;

map<char ,int>charMap;           

map<int ,char >intMap;

Two, map add data

 map<int ,string> maplive;  
 1.pair<int,string> value(1,"a");maplive.insert(value);

    Equivalent to maplive.insert (P AIR <int, String> (. 1, "A"));

2. maplive.insert(map<int,string>::value_type(1,"a"));

3. maplive [1] = "a"; // map the simplest and most commonly used to insert added!

Three, map basic operation functions:

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

 

 1 #include <iostream>
 2 #include "string.h"
 3 #include "stdio.h"
 4 #include<map>
 5 using namespace std;
 6 
 7 int main(){
 8     map<string,int> strMap;  //以string为键值,以int为实值
 9     strMap[string("jjhou")] = 1;
10     strMap[string("jerry")] = 2;
11     strMap[string("jason")] = 3;
12     strMap[string("jimmy")] = 4;
13 
14     pair<string,int> value(string("david"),5);
15     strMap.insert(value);//插入新元素
16 
17     map<string,int>::iterator strmap_iter = strMap.begin();
18     for(;strmap_iter !=strMap.end();strmap_iter++)
19     {
20         cout<<strmap_iter->first<<' '<<strmap_iter->second<<endl;
21     }
22     cout<<endl;
23 
24     int number = strMap[string("jjhou")];
25     cout<<number<<endl;
26     cout<<endl;
27 
28     //查找元素
29     map<string,int> :: Iterator iter1;
 30      // face associative containers, they provide should use the find function to search for elements than using the STL algorithms find () more efficient. Because the STL algorithms find () loop just search. 
31 is      iter1 strMap.find = ( String ( " mchen " ));
 32      IF (iter1 == strMap.end ())
 33 is          COUT << " mchen NO Fount " << endl;
 34 is          COUT << endl;
 35  
36      iter1 = strMap.find ( String ( " Jerry " ));
 37 [      IF (! = iter1 strMap.end ())
 38 is         << COUT " Jerry Fount " << endl;
 39          COUT << endl;
 40  
41 is      // modify real value, can not be modified key 
42 is      iter1-> SECOND = . 9 ; // can modify the "value" (not by the map iterator Key) 
43 is      int number1 = [strMap String ( " Jerry " )];
 44 is      COUT number1 << << endl;
 45      
46 is      // delete elements 
47      Map < String , int > :: = Iterator strmap_iter1 strMap.begin ();
 48      for(; strmap_iter1 strMap.end = ();! strmap_iter1 ++ )
 49      {
 50          COUT strmap_iter1- <<> << First '  ' << strmap_iter1-> SECOND << endl;
 51 is      }
 52 is      COUT << endl;
 53 is      
54 is      strMap.erase (iter1); // delete an entry 
55      strMap.erase ( String ( " Jason " )); // delete The key 
56 is  
57 is      Map < String , int > :: = Iterator strmap_iter2 strMap.begin ();
 58      for(;strmap_iter2 !=strMap.end();strmap_iter2++)
59     {
60         cout<<strmap_iter2->first<<' '<<strmap_iter2->second<<endl;
61     }
62 }

 

Guess you like

Origin www.cnblogs.com/Toya/p/11128582.html