fork download
  1. #include <cstdio>
  2. #include <vector>
  3. #include <algorithm>
  4.  
  5. void print_vector(const char* msg, const std::vector<int> & v) {
  6. printf("%s :", msg);
  7. for (auto x : v)
  8. printf(" %d", x);
  9. printf("\n");
  10. }
  11.  
  12. int main() {
  13. int x;
  14. std::vector<int> v;
  15. while (scanf("%d", &x) == 1) v.push_back(x);
  16.  
  17. print_vector("Before unique", v);
  18.  
  19. // unique
  20. std::sort(v.begin(), v.end());
  21. auto it = std::unique(v.begin(), v.end());
  22. v.resize(it-v.begin());
  23.  
  24. print_vector("After unique", v);
  25.  
  26. return 0;
  27. }
  28.  
Success #stdin #stdout 0s 3468KB
stdin
4 2 1 3 1 2 4 5
stdout
Before unique : 4 2 1 3 1 2 4 5
After unique : 1 2 3 4 5