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

STL

开发平台:

C/C++

  1. //文件名:CHAPTER10-30.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. int main( )
  8. {
  9.    typedef multimap <int, int, less<int> > IntMMap;
  10.    IntMMap m1;
  11.    multimap <int, int> :: const_iterator m1_RcIter;
  12.    typedef pair <int, int> Int_Pair;
  13.    m1.insert ( Int_Pair ( 1, 10 ) );
  14.    m1.insert ( Int_Pair ( 2, 20 ) );
  15.    m1.insert ( Int_Pair ( 3, 30 ) );
  16.    pair <IntMMap::const_iterator, IntMMap::const_iterator> p1, p2;
  17.    p1 = m1.equal_range( 2 );
  18.    cout << "The lower bound of the element with "
  19.         << "a key of 2 in the multimap m1 is: "<< p1.first -> second << "." << endl;
  20.    cout << "The upper bound of the element with "
  21.         << "a key of 2 in the multimap m1 is: "<< p1.second -> second << "." << endl;
  22.    // Compare the upper_bound called directly 
  23.    m1_RcIter = m1.upper_bound( 2 );
  24.    cout << "A direct call of upper_bound( 2 ) gives "
  25.         << m1_RcIter -> second << "," << endl<< " matching the 2nd element of the pair"
  26.         << " returned by equal_range( 2 )." << endl;
  27.    p2 = m1.equal_range( 4 );
  28.    // If no match is found for the key, both elements of the pair return end( )
  29.    if ( ( p2.first == m1.end( ) ) && ( p2.second == m1.end( ) ) )
  30.       cout << "The multimap m1 doesn't have an element "<< "with a key less than 4." << endl;
  31.    else
  32.       cout << "The element of multimap m1 with a key >= 40 is: "<< p1.first -> first << "." << endl;
  33. }