STL源码剖析之算法:partial_sum

  
  
  
  
  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 __partial_sum(InputIterator first, InputIterator last,             
  9.                              OutputIterator result, T*, BinaryOperation binary_op) { 
  10.     T value = *first;                                                             
  11.     while(++first != last) {                                                      
  12.         value = binary_op(value, *first);                                         
  13.         *++result = value;                                  
  14.     }                                                                             
  15.     return ++result;                                                              
  16. }                                                                                 
  17.                                                                                   
  18. template <class InputIterator, class OutputIterator, class BinaryOperation>       
  19. OutputIterator partial_sum(InputIterator first, InputIterator last,               
  20.                            OutputIterator result, BinaryOperation binary_op) {    
  21.     if(first == last) return result;                                              
  22.     *result = *first;                                                             
  23.     return __partial_sum(first, last, result, value_type(first), binary_op);      

 

你可能感兴趣的:(STL,partial_sum)