fork download
  1. #include <random>
  2. #include <iostream>
  3. #include <iomanip>
  4. #include <vector>
  5. #include <algorithm>
  6. #include <string>
  7.  
  8. template <typename container_type>
  9. void unique_sort(container_type & c)
  10. {
  11. auto begin = std::begin(c);
  12. auto end = std::end(c);
  13.  
  14. std::sort(begin, end);
  15. c.erase(std::unique(begin, end), end);
  16. }
  17.  
  18. std::vector<std::string> randomized_string_vector(std::size_t elements)
  19. {
  20. std::mt19937 rng((std::random_device())());
  21. std::uniform_int_distribution<int> alpha('a', 'e');
  22.  
  23. std::vector <std::string> v;
  24. for (unsigned i = 0; i < elements; ++i)
  25. {
  26. std::string element;
  27. for (unsigned j = 0; j < 2; ++j)
  28. element += (char)alpha(rng);
  29. v.emplace_back(std::move(element));
  30. }
  31.  
  32. return v;
  33. }
  34.  
  35. template <typename container_type>
  36. void print(const container_type& c)
  37. {
  38. const unsigned perLine = 10;
  39. unsigned count = 0;
  40.  
  41. for (const auto& e : c)
  42. {
  43. std::cout << std::setw(80 / perLine) << e;
  44. if (++count == perLine)
  45. {
  46. std::cout << '\n';
  47. count = 0;
  48. }
  49. }
  50. std::cout << '\n';
  51. }
  52.  
  53. int main()
  54. {
  55. auto v = randomized_string_vector(40);
  56. std::cout << "original vector size: " << v.size() << '\n';
  57. std::cout << "original vector:\n";
  58. print(v);
  59.  
  60. unique_sort(v);
  61. std::cout << "vector size after sort: " << v.size() << '\n';
  62. std::cout << "vector after sort:\n";
  63. print(v);
  64. }
  65.  
Success #stdin #stdout 0s 3480KB
stdin
Standard input is empty
stdout
original vector size: 40
original vector:
      ed      ed      bc      be      db      ac      ea      aa      ad      bb
      cb      ea      db      dd      ec      ec      be      be      ba      db
      de      dd      cc      ea      ca      eb      db      ca      ec      be
      ad      ad      ca      bd      ac      bc      eb      aa      ab      be

vector size after sort: 19
vector after sort:
      aa      ab      ac      ad      ba      bb      bc      bd      be      ca
      cb      cc      db      dd      de      ea      eb      ec      ed