fork download
  1. #include <list>
  2. #include <set>
  3. #include <algorithm>
  4. #include <iostream>
  5.  
  6. using namespace std;
  7.  
  8. template<typename Iterator>
  9. void print(Iterator begin, Iterator end)
  10. {
  11. cout << "( " ;
  12. for (auto it = begin; it != end; ++it)
  13. cout << *it << " ";
  14. cout << ")" << endl;
  15. }
  16.  
  17. template<typename T>
  18. void print(const T & values)
  19. {
  20. print(values.begin(), values.end());
  21. }
  22.  
  23. bool not_equal(int a, int b)
  24. {
  25. return a != b;
  26. }
  27.  
  28. int main( )
  29. {
  30. using namespace std;
  31. list<int> values;
  32.  
  33. values.push_back( 50 );
  34. values.push_back( 40 );
  35. values.push_back( 40 );
  36. values.push_back( 40 );
  37. values.push_back( 10 );
  38. values.push_back( 20 );
  39. values.push_back( 20 );
  40. values.push_back( 20 );
  41.  
  42. print(values);
  43.  
  44. list<int> out;
  45. auto it = adjacent_find(values.begin(), values.end());
  46. while (it != values.end())
  47. {
  48. out.push_back(*it);
  49. ++it;
  50. it = adjacent_find(it, values.end());
  51. }
  52.  
  53. print(out);
  54.  
  55. {
  56. auto v = values;
  57. cout << "unique" << endl;
  58. print(v);
  59. auto e = unique(v.begin(), v.end());
  60. print(v.begin(), e);
  61. }
  62.  
  63. {
  64. auto v = values;
  65. cout << "not unique" << endl;
  66. print(v);
  67. auto e = unique(v.begin(), v.end(), not_equal);
  68. print(v.begin(), e);
  69. }
  70.  
  71. {
  72. auto v = values;
  73. cout << endl << "custom" << endl;
  74. print(v);
  75. v.sort();
  76. cout << "sorted" << endl;
  77. print(v);
  78.  
  79. set<int> out;
  80.  
  81. auto prev = v.begin();
  82. auto current = ++v.begin();
  83. for (; current != v.end(); ++current)
  84. {
  85. if (*prev == *current)
  86. out.insert(*prev);
  87. ++prev;
  88. }
  89.  
  90. print(out);
  91. }
  92.  
  93. return 0;
  94. }
  95.  
Success #stdin #stdout 0s 3276KB
stdin
Standard input is empty
stdout
( 50 40 40 40 10 20 20 20 )
( 40 40 20 20 )
unique
( 50 40 40 40 10 20 20 20 )
( 50 40 10 20 )
not unique
( 50 40 40 40 10 20 20 20 )
( 50 )

custom
( 50 40 40 40 10 20 20 20 )
sorted
( 10 20 20 20 40 40 40 50 )
( 20 40 )