lower_bound()函数返回能够插入value的第一个位置,该函数要求目标序列必须是有序的

  1. template <class ForwardIterator, class T>                                         
  2. inline ForwardIterator lower_bound(ForwardIterator first,                         
  3.                                    ForwardIterator last,                          
  4.                                    const T &value) {                              
  5.     return __lower_bound(first, last, value,                                    
  6.                          distance_type(first), iterator_category(first));       
  1. // forward iterator 版本 
  2. template <class ForwardIterator, class T, class Distance>                         
  3. ForwardIterator __lower_bound(ForwardIterator first,                              
  4.                               ForwardIterator last,                               
  5.                               const T &value,                                     
  6.                               Distance*,                                          
  7.                               forward_iterator_tag) {                             
  8.     Distance len = 0;                                                             
  9.     distance(first, last, len);                                                   
  10.     Distance half;                                                                
  11.     ForwardIterator middle;                                                       
  12.                                                                                   
  13.     while(len > 0) {                                                              
  14.         half = len >> 1;                                                          
  15.         advance(middle, half);                                                    
  16.                                                                                   
  17.         if(*middle < value) {                                                     
  18.             first = middle;                                                       
  19.             ++first;                                                              
  20.             len = len - half - 1;                                                 
  21.         } else                                                                    
  22.             len = half;                                                           
  23.     }                                                                             
  24.                                                                                   
  25.     return first;                                                                 
  1. // random access iterator 版本 
  2. template <class RandomAccessIterator, class T, class Distance>                    
  3. RandomAccessIterator __lower_bound(RandomAccessIterator first,                    
  4.                                    RandomAccessIterator last,                     
  5.                                    const T &value,                                
  6.                                    Distance*,                                     
  7.                                    random_access_iterator_tag) {                  
  8.     Distance len = last - first;                                                  
  9.     Distance half;                                                                
  10.     RandomAccessIterator middle;                                                  
  11.                                                                                   
  12.     while(len > 0) {                                                              
  13.         half = len >> 1;                                                          
  14.         middle = first + half;                                                    
  15.                                                                                   
  16.         if(*middle < value) {                                                     
  17.             first = middle + 1;                                                   
  18.             len = len - half - 1;                                                 
  19.         } else                                                                    
  20.             len = half;                                                           
  21.     }                                                                             
  22.                                                                                   
  23.     return first;                                                                 
  24. }