Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

Untitled

By: a guest on Mar 14th, 2013  |  syntax: None  |  size: 0.60 KB  |  views: 29  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
This paste has a previous version, view the difference. Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. template<typename it>
  2. it yourUnique(it begin, it end){
  3.     typedef typename std::iterator_traits<it>::value_type value_t;
  4.     typedef std::pair<value_t,int> pair_t;
  5.  
  6.     vector<pair_t> v;
  7.     for(it c = begin; c != end; ++c){
  8.         v.push_back(make_pair(*c, v.size())); // second is start index;
  9.     }
  10.     sort(v.begin(), v.end()); // sort by value then by index
  11.     v.erase(unique(v.begin(), v.end(), firstEqual<value_t>), v.end());
  12.     sort(v.begin(), v.end(), bySecond<value_t>); // restore order.
  13.     it c = begin;
  14.  
  15.     for(const auto& x: v){
  16.        *(c++) = x.first;
  17.     }
  18.     return c;
  19. }