fork download
  1. #include <algorithm>
  2. #include <iostream>
  3. #include <vector>
  4.  
  5. template <typename T>
  6. std::vector<std::pair<T, std::size_t>>
  7. adjacent_count(const std::vector<T>& v)
  8. {
  9. std::vector<std::pair<T, std::size_t>> res;
  10.  
  11. for (auto it = v.begin(), e = v.end(); it != e; /*Empty*/) {
  12. auto it2 = std::adjacent_find(it, e, std::not_equal_to<>{});
  13. if (it2 != e) {
  14. ++it2;
  15. }
  16. res.emplace_back(*it, std::distance(it, it2));
  17. it = it2;
  18. }
  19. return res;
  20. }
  21.  
  22. int main() {
  23. const std::vector<int> v{ 1, 1, 2, 3, 3, 3, 1, 1 };
  24.  
  25. for (const auto& p : adjacent_count(v)) {
  26. std::cout << p.first << ":" << p.second << std::endl;
  27. }
  28. }
  29.  
Success #stdin #stdout 0s 3468KB
stdin
Standard input is empty
stdout
1:2
2:1
3:3
1:2