STL源码剖析之算法:set_union & set_intersection

  
  
  
  
  1. template <class InputIterator1, class InputIterator2, class OutputIterator>       
  2. OutputIterator set_union(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.         } else if(*first2 < *first1) {                                            
  10.             *result = *first2;                                                    
  11.             ++first2;                                                             
  12.         } else {                                                                  
  13.             *result = *first1;                                                    
  14.             ++first1;                                                             
  15.             ++first2;                                                             
  16.         }                                                                         
  17.         ++result;                                                                 
  18.     }                                                                             
  19.                                                                                   
  20.     return copy(first1, last1, copy(first2, last2, result));                      

 

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

 

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