数组相减

   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: {
   6:   while (first1!=last1 && first2!=last2)
   7:   {
   8:     if (*first1<*first2) 
   9:     { 
  10:         *result = *first1; 
  11:         ++result; 
  12:         ++first1; 
  13:     }
  14:     else if (*first2<*first1) 
  15:     {
  16:         ++first2;
  17:     }
  18:     else 
  19:     { 
  20:         ++first1; 
  21:         ++first2; 
  22:     }
  23:   }
  24:   return std::copy(first1,last1,result);
  25: }

两个数组,先做好排序,

然后分别从头对比,对于两集合中相同的元素,就添加到result集合中去,否则继续调整两个集合的指针,以便找到下一个可能相同的位置。

posted @ 2013-12-23 15:22  Daniel King  阅读(543)  评论(0编辑  收藏  举报