fork download
  1. #include <vector>
  2. #include <algorithm>
  3. #include <iostream>
  4.  
  5. int main()
  6. {
  7. using namespace std;
  8.  
  9. vector<int> v = { 1, 4, 7, 8, 11 };
  10. vector<vector<int>> result;
  11.  
  12. for (auto i = v.begin(), e = v.end(); i != e; ) {
  13. auto f = adjacent_find(i, e, [](auto l, auto r) -> bool { return (r - l) >= 2; });
  14. if (f != e) ++f;
  15. result.emplace_back(i, f);
  16. i = f;
  17. }
  18.  
  19. for (auto && i : result ) {
  20. for (auto && j : i)
  21. cout << j << " , ";
  22. cout << "\n";
  23. }
  24. }
  25.  
Success #stdin #stdout 0s 15240KB
stdin
Standard input is empty
stdout
1 , 
4 , 
7 , 8 , 
11 ,