STL源码剖析之算法:adjacent_difference

  
  
  
  
  1. template <class Iterator>    
  2. typename iterator_traits<Iterator>::value_type*
  3. value_type(Iterator &iterator) {
  4.     return static_cast<typename iterator_traits<Iterator>::value_type*>(0);
  5. }                                                                                    
  6.                                                                                      
  7. template <class InputIterator, class OutputIterator, class T, class BinaryOperation> 
  8. OutputIterator __adjacent_difference(InputIterator first, InputIterator last,
  9.                                      OutputIterator result, T*, BinaryOperation binary_op) {
  10. T value = *first;                                                                
  11.     while(++first != last) {                                                         
  12.         T tmp = *first;                                                              
  13.         *++result = binary_op(tmp, value);                                             
  14.         value = tmp;                                                                 
  15.     }                                                                                
  16.                                                                                      
  17.     return ++result;                                                                 
  18. }                                                                                    
  19.                                                                                      
  20. template <class InputIterator, class OutputIterator, class BinaryOperation>          
  21. OutputIterator adjacent_difference(InputIterator first, InputIterator last,          
  22.                                    OutputIterator result, BinaryOperation binary_op) { 
  23.     if(first == last) return result;                                                 
  24.     *result = *first;                                              
  25.     return __adjacent_difference(first, last, result, value_type(first), binary_op); 

 

你可能感兴趣的:(STL)