fork download
  1. #include <iostream>
  2. #include <vector>
  3.  
  4.  
  5. template<typename X>
  6. inline void print(X && x) {
  7. auto first = std::begin(x);
  8. auto last = std::end(x);
  9. while (first != last) {
  10. auto next = first + 1;
  11. while (next != last && next - first == *next - *first){
  12. ++next;
  13. }
  14. std::cout << *first;
  15. if (next - first > 1) {
  16. std::cout << "-" << *first + next - first - 1;
  17. }
  18. if (next != last) {
  19. std::cout << ",";
  20. }
  21. first = next;
  22. }
  23. std::cout << std::endl;
  24. }
  25.  
  26. inline void print(std::initializer_list<int> x) {
  27. print<std::initializer_list<int>>(std::move(x));
  28. }
  29.  
  30. int main(){
  31. int a[] = { 1, 2, 3, 4, 7, 9, 10 };
  32. print(a);
  33. auto v = std::vector<int>{ 1, 2, 3, 4, 7, 9, 10 };
  34. print(v);
  35. print({ 1, 2, 3, 4, 7, 9, 10 });
  36. return 0;
  37. }
Success #stdin #stdout 0s 3416KB
stdin
Standard input is empty
stdout
1-4,7,9-10
1-4,7,9-10
1-4,7,9-10