STL源码剖析之算法:set_difference & set_symmetric_difference

  
  
  
  
  1. template <class InputIterator1, class InputIterator2, class OutputIterator>       
  2. OutputIterator set_difference(InputIterator1 first1, InputIterator1 last1,        
  3.                                 InputIterator2 first2, InputIterator2 last2,      
  4.                                 OutputIterator result) {                          
  5.     while(first1 != last1 && first2 != last2) {                                   
  6.         if(*first1 < *first2) {                                                   
  7.             *result = *first1;                                                    
  8.             ++first1;                                                             
  9.             ++result;                                                             
  10.         } else if(*first2 < *first1) {                                            
  11.             ++first2;                                                             
  12.         } else {                                                                  
  13.             ++first1;                                                             
  14.             ++first2;                                                             
  15.         }                                                                         
  16.     }                                                                             
  17.                                                                                   
  18.     return copy(first1, last1, result);                      

 

  
  
  
  
  1. template <class InputIterator1, class InputIterator2, class OutputIterator>       
  2. OutputIterator Set_symmetric_difference(InputIterator1 first1, InputIterator1 last1, 
  3.                                         InputIterator2 first2, InputIterator2 last2, 
  4.                                         OutputIterator result) {                  
  5.     while(first1 != last1 && first2 != last2) {                                   
  6.         if(*first1 < *first2) {                                                   
  7.             *result = *first1;                                                    
  8.             ++first1;                                                             
  9.             ++result;                                                             
  10.         } else if(*first2 < *first1) {                                            
  11.             *result = *first2;                                                    
  12.             ++first2;                                                             
  13.             ++result;                             
  14.         } else {                                                                  
  15.             ++first1;                                                             
  16.             ++first2;                                                             
  17.         }                                                                         
  18.     }                                                                             
  19.                                                                                   
  20.     return copy(first2, last2, copy(first1, last1, result));                      

 

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