stl map中的lower_bound和 upper_bound

时间:2022-09-28 18:15:51

map中的lower_bound和upper_bound的意思其实很简单,就两句话:

map::lower_bound(key):返回map中第一个大于或等于key的迭代器指针

map::upper_bound(key):返回map中第一个大于key的迭代器指针

所以,理解这两个函数请不要按照字面意义思考太复杂,因为仅仅是不小于(lower_bound)和大于(upper_bound)这么简单。

看两个msdn里的例子

// map_upper_bound.cpp
// compile with: /EHsc
#include <map>
#include <iostream>

int main( )
{
   using namespace std;
   map <int, int> m1;
   map <int, int> :: const_iterator m1_AcIter, m1_RcIter;
   typedef pair <int, int> Int_Pair;

   m1.insert ( Int_Pair ( 1, 10 ) );
   m1.insert ( Int_Pair ( 2, 20 ) );
   m1.insert ( Int_Pair ( 3, 30 ) );
 
 // 返回m1中第一个key值大于或等于2的元素的迭代器,当然是<2,20>
 m1_RcIter = m1.upper_bound( 2 ); cout << "The first element of map m1 with a key " << "greater than 2 is: " << m1_RcIter -> second << "." << endl; // If no match is found for the key, end is returned m1_RcIter = m1. upper_bound ( 4 );
   // m1中key值并没有大于或等于4的     if ( m1_RcIter == m1.end( ) ) cout << "The map m1 doesn't have an element " << "with a key greater than 4." << endl; else cout << "The element of map m1 with a key > 4 is: " << m1_RcIter -> second << "." << endl; // The element at a specific location in the map can be found // using a dereferenced iterator addressing the location m1_AcIter = m1.begin( ); m1_RcIter = m1. upper_bound ( m1_AcIter -> first ); cout << "The 1st element of m1 with a key greater than\n" << "that of the initial element of m1 is: " << m1_RcIter -> second << "." << endl; }
The first element of map m1 with a key greater than 2 is: 30.
The map m1 doesn't have an element with a key greater than 4.
The 1st element of m1 with a key greater than
that of the initial element of m1 is: 20.


// map_lower_bound.cpp
// compile with: /EHsc
#include <map>
#include <iostream>

int main( )
{
   using namespace std;   
   map <int, int> m1;
   map <int, int> :: const_iterator m1_AcIter, m1_RcIter;
   typedef pair <int, int> Int_Pair;

   m1.insert ( Int_Pair ( 1, 10 ) );
   m1.insert ( Int_Pair ( 2, 20 ) );
   m1.insert ( Int_Pair ( 3, 30 ) );
  //key值大于2的是<3,30>
   m1_RcIter = m1.lower_bound( 2 );
   cout << "The first element of map m1 with a key of 2 is: "
        << m1_RcIter -> second << "." << endl;

   // If no match is found for this key, end( ) is returned
   m1_RcIter = m1. lower_bound ( 4 );

   if ( m1_RcIter == m1.end( ) )
      cout << "The map m1 doesn't have an element "
           << "with a key of 4." << endl;
   else
      cout << "The element of map m1 with a key of 4 is: "
           << m1_RcIter -> second << "." << endl;

   // The element at a specific location in the map can be found 
   // using a dereferenced iterator addressing the location
   m1_AcIter = m1.end( );
   m1_AcIter--;
   m1_RcIter = m1. lower_bound ( m1_AcIter -> first );
   cout << "The element of m1 with a key matching "
        << "that of the last element is: "
        << m1_RcIter -> second << "." << endl;
}

The first element of map m1 with a key of 2 is: 20.

The map m1 doesn't have an element with a key of 4.

The element of m1 with a key matching that of the last element is: 30.