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. const auto it2 = std::find_if(it, e, [&](const auto&x) { return x != *it; });
  13. res.emplace_back(*it, std::distance(it, it2));
  14. it = it2;
  15. }
  16. return res;
  17. }
  18.  
  19. int main() {
  20. const std::vector<int> v{ 1, 1, 2, 3, 3, 3, 1, 1 };
  21.  
  22. for (const auto& p : adjacent_count(v)) {
  23. std::cout << p.first << ":" << p.second << std::endl;
  24. }
  25. }
  26.  
Success #stdin #stdout 0s 3472KB
stdin
Standard input is empty
stdout
1:2
2:1
3:3
1:2