how to use the map function blanket ↓↓↓

Map is a standard container c ++, she provided a good one to one relationship, based on a map in some programs can play a multiplier effect, summarizes some basic, simple and practical operation map!

1. map最基本的构造函数;
   map<string , int >mapstring;         map<int ,string >mapint;
   map<sring, char>mapstring;         map< char ,string>mapchar;
   map<char ,int>mapchar;            map<int ,char >mapint;

2. map data is added;

   Map <int, String> maplive;  
   1. maplive.insert (pair <int, String> (102, "aclive"));
   2. maplive.insert (Map <int, String> :: the value_type (321, "Hai" ));
   . 3, maplive [112] = "April"; Map simplest most common insertion // add!
3, the elements map to find:

  find () function returns an iterator to key into key elements, if not found returns an iterator pointing to the tail of the map.        

   Map <int, String> :: Iterator l_it ;; 
   l_it = maplive.find (112);
   IF (l_it maplive.end == ())
                COUT << "WE do Not Find 112" << endl;
   the else << COUT "Find 112 WO" << endl;
. 4, elements deleting map:
   If you delete 112;
   Map <int, String> :: Iterator l_it ;;
   l_it = maplive.find (112);
   IF (l_it == maplive.end ())
        COUT << "WE do not Find 112" << endl;
   the else maplive. ERASE (l_it); // Delete 112;
. 5, in the swap map usage:
  the map is not a swap container exchange element, but two vessels exchanged;
  the For Example:

  #include <map>
  #include <iostream>
  using namespace std;
  int main( )
  {
      map <int, int> m1, m2, m3;
      map <int, int>::iterator m1_Iter;
      m1.insert ( pair <int, int>  ( 1, 10 ) );
      m1.insert ( pair <int, int>  ( 2, 20 ) );
      m1.insert ( pair <int, int>  ( 3, 30 ) );
      m2.insert ( pair <int, int>  ( 10, 100 ) );
      m2.insert ( pair <int, int>  ( 20, 200 ) );
      m3.insert ( pair <int, int>  ( 30, 300 ) );
   cout << "The original map m1 is:";
   for ( m1_Iter = m1.begin( ); m1_Iter != m1.end( ); m1_Iter++ )
      cout << " " << m1_Iter->second;
      cout   << "." << endl;
   // This is the member function version of swap
   //m2 is said to be the argument map; m1 the target map
   m1.swap( m2 );
   cout << "After swapping with m2, map m1 is:";
   for ( m1_Iter = m1.begin( ); m1_Iter != m1.end( ); m1_Iter++ )
      cout << " " << m1_Iter -> second;
      cout  << "." << endl;
   cout << "After swapping with m2, map m2 is:";
   for ( m1_Iter = m2.begin( ); m1_Iter != m2.end( ); m1_Iter++ )
      cout << " " << m1_Iter -> second;
      cout  << "." << endl;
   // This is the specialized template version of swap
   swap( m1, m3 );
   cout << "After swapping with m3, map m1 is:";
   for ( m1_Iter = m1.begin( ); m1_Iter != m1.end( ); m1_Iter++ )
      cout << " " << m1_Iter -> second;
      cout   << "." << endl;
}

 

6.map sort of problem:
 the Map elements are automatically sorted in ascending order by key, it is not on the map with the sort function:
  the For Example:

  #include <map>
  #include <iostream>
  using namespace std;
 int main( )
 {
   map <int, int> m1;
   map <int, int>::iterator m1_Iter;
   m1.insert ( pair <int, int>  ( 1, 20 ) );
   m1.insert ( pair <int, int>  ( 4, 40 ) );
   m1.insert ( pair <int, int>  ( 3, 60 ) );
   m1.insert ( pair <int, int>  ( 2, 50 ) );
   m1.insert ( pair <int, int>  ( 6, 40 ) );
   m1.insert ( pair <int, int>  ( 7, 30 ) );
   cout << "The original map m1 is:"<<endl;
   for ( m1_Iter = m1.begin( ); m1_Iter != m1.end( ); m1_Iter++ )
      cout <<  m1_Iter->first<<" "<<m1_Iter->second<<endl;
  
}

 

  Original M1 IS Map of The:
  . 1 20 is
  2 50
  . 3 60
  . 4 40
  . 6 40
  . 7 30
  Press any key to continue.

7, the basic operation of the map functions:
      C ++ is an association Maps container, comprising a "key / value" to
      begin ()           returns a pointer to the head of the map iterator
      clear ()         Removes all elements
      count ()           Returns the specified element appears the number of
      empty ()           if the map true to return null
      end ()             returns a pointer to the end of the map iterators
      equal_range ()     returns a special entry iterator to
      erase ()          removes an element
      find ()            to find an element
      get_allocator ()  returns the map configuration an
      insert ()          insert elements
      key_comp ()        function returns the key elements of Comparative
      lower_bound ()     returns the key-value> = a first position to a given element
      MAX_SIZE ()        returns the maximum number of elements can be accommodated
     rbegin ()          returns a pointer to the tail map reverse iterator
     rend ()           Returns a pointer to the head of reverse iterator map
     size ()            returns the number of elements in the map
     swap ()             exchanging two map
      upper_bound, ()      Returns the key> element to the first position of a given
      value_comp ()       Returns the value of the comparison element function

//forward from:

Slightly modified for easy reading.

Guess you like

Origin www.cnblogs.com/ljy-endl/p/11403910.html
Recommended