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; }