fork download
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4.  
  5. using std::cout;
  6. using std::endl;
  7.  
  8. using std::vector;
  9. using std::unique;
  10. using std::distance;
  11.  
  12. void unique_output(const vector<int>& v){
  13. vector<int> vv(v);
  14. auto it = unique(vv.begin(), vv.end());
  15. vv.resize(distance(vv.begin(), it));
  16. for_each(vv.begin(), vv.end(), [](int w){
  17. cout << w << ' ';
  18. });
  19. cout << endl;
  20. }
  21.  
  22. int main(){
  23. vector<int> a = {1, 2, 3, 4, 6, 5, 2, 3, 4, 1, 2, 5, 1, 3, 6, 5};
  24. vector<int> b = {1, 2, 3, 4, 4, 6, 5, 5, 2, 2, 2, 2, 2, 3, 4, 3, 2, 1};
  25.  
  26. unique_output(a);
  27. unique_output(b);
  28.  
  29. return 0;
  30. }
  31.  
Success #stdin #stdout 0s 3432KB
stdin
Standard input is empty
stdout
1 2 3 4 6 5 2 3 4 1 2 5 1 3 6 5 
1 2 3 4 6 5 2 3 4 3 2 1