fork(3) download
  1. #include <vector>
  2. #include <iostream>
  3.  
  4. std::vector<std::pair<int, int>> rle(const std::vector<int>& in)
  5. {
  6. std::vector<std::pair<int, int>> out;
  7. for (int i: in)
  8. {
  9. if (out.empty() || out.back().first != i)
  10. {
  11. out.emplace_back(std::make_pair(i, 1));
  12. }
  13. else
  14. {
  15. ++out.back().second;
  16. }
  17. }
  18. return out;
  19. }
  20.  
  21. int main()
  22. {
  23. auto out = rle({1,1,1,3,4,2,2,2,2,51});
  24. for (const auto& pair : out)
  25. {
  26. std::cout << pair.first << ": " << pair.second << ", ";
  27. }
  28. std::cout << std::endl;
  29. }
  30.  
Success #stdin #stdout 0s 3468KB
stdin
Standard input is empty
stdout
1: 3, 3: 1, 4: 1, 2: 4, 51: 1,