STL unique

   1: template <class ForwardIterator>
   2:   ForwardIterator unique (ForwardIterator first, ForwardIterator last)
   3: {
   4:   if (first==last) 
   5:     return last;
   6:  
   7:   ForwardIterator result = first;
   8:   while (++first != last)
   9:   {
  10:     if (!(*result == *first))  // or: if (!pred(*result,*first)) for version (2)
  11:       *(++result)=*first;
  12:   }
  13:   return ++result;
  14: }

集合先要进行排序,然后依次取一个元素放到result中,并且找到原集合中的下一个不等于result当前元素的位置,再进行上述操作。

转载于:https://www.cnblogs.com/long123king/p/3487515.html

猜你喜欢

转载自blog.csdn.net/weixin_33816946/article/details/94503582