CHAPTER10-17.cpp
上传用户:fjc899
上传日期:2007-07-03
资源大小:187k
文件大小:2k
源码类别:

STL

开发平台:

C/C++

  1. //文件名:CHAPTER10-17.cpp
  2. #include <map>
  3. #include <iostream>
  4. #if _MSC_VER > 1020   // if VC++ version is > 4.2
  5.    using namespace std;  // std c++ libs implemented in std
  6. #endif
  7. void main( )
  8. {
  9.    map <int, int>::iterator m1_pIter, m2_pIter;
  10.    map <int, int> m1, m2;
  11.    typedef pair <int, int> Int_Pair;
  12.    m1.insert ( Int_Pair ( 1, 10 ) );
  13.    m1.insert ( Int_Pair ( 2, 20 ) );
  14.    m1.insert ( Int_Pair ( 3, 30 ) );
  15.    m1.insert ( Int_Pair ( 4, 40 ) );
  16.    cout << "The original key values of m1 =";
  17.    for ( m1_pIter = m1.begin( ); m1_pIter != m1.end( ); m1_pIter++ )
  18.       cout << " " << m1_pIter -> first;
  19.    cout << "." << endl<< "The original mapped values of m1 =";
  20.    for ( m1_pIter = m1.begin( ); m1_pIter != m1.end( ); m1_pIter++ )
  21.       cout << " " << m1_pIter -> second;
  22.    cout << "." << endl;
  23.    pair< map<int,int>::iterator, bool > pr;
  24.    pr = m1.insert ( Int_Pair ( 1, 10 ) );
  25.    if( pr.second == true )   
  26.    {      cout << "The element 10 was inserted in m1 successfully." << endl;   }
  27.    else   
  28.    {      cout << "The element 10 already exists in m1n"
  29.            << "with a key value of ( (pr.first) -> first ) = " 
  30.            << ( pr.first ) -> first << "." << endl;   }
  31.    // The hint version of insert
  32.    m1.insert( --m1.end( ), Int_Pair ( 5, 50 ) );
  33.    cout << "After the insertions, the key values of m1 =";
  34.    for ( m1_pIter = m1.begin( ); m1_pIter != m1.end( ); m1_pIter++ )
  35.       cout << " " << m1_pIter -> first;
  36.    cout << "," << endl;
  37.    cout << "and the mapped values of m1 =";
  38.    for ( m1_pIter = m1.begin( ); m1_pIter != m1.end( ); m1_pIter++ )
  39.       cout << " " << m1_pIter -> second;
  40.    cout << "." << endl;
  41.    m2.insert ( Int_Pair ( 10, 100 ) );
  42.    cout << "After the insertions, the key values of m2 =";
  43.    for ( m2_pIter = m2.begin( ); m2_pIter != m2.end( ); m2_pIter++ )
  44.       cout << " " << m2_pIter -> first;
  45.    cout << "," << endl<< "and the mapped values of m2 =";
  46.    for ( m2_pIter = m2.begin( ); m2_pIter != m2.end( ); m2_pIter++ )
  47.       cout << " " << m2_pIter -> second;
  48.    cout << "." << endl;
  49. }