fork download
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4.  
  5. int main()
  6. {
  7. typedef std::vector<int> vec_type;
  8.  
  9. vec_type numbers { 10, 10, 20, 20, 20, 30, 30, 30, 40, 30, 10, 10 };
  10.  
  11. auto it = std::begin(numbers);
  12.  
  13. while ((it = std::adjacent_find(it, std::end(numbers))) != std::end(numbers))
  14. {
  15. auto value = *it;
  16. unsigned matches = 1;
  17.  
  18. while (++it != std::end(numbers) && *it == value)
  19. ++matches;
  20.  
  21. std::cout << value << ": " << matches << " matches.\n" ;
  22. }
  23. }
Success #stdin #stdout 0s 3428KB
stdin
Standard input is empty
stdout
10: 2 matches.
20: 3 matches.
30: 3 matches.
10: 2 matches.