fork download
  1. #include<iostream>
  2. #include<vector>
  3. #include<cstdlib>
  4. #include<algorithm>
  5. using namespace std;
  6.  
  7. template<class T, class Pred>
  8. vector<vector<T>> divide(const vector<T>& v, Pred pred)
  9. {
  10. vector<vector<T>> ret;
  11. auto i = v.begin();
  12. for(;;) {
  13. auto j = adjacent_find(i, v.end(), pred);
  14. if (j == v.end())
  15. break;
  16. ret.emplace_back(i, j + 1);
  17. i = j + 1;
  18. }
  19. ret.emplace_back(i, v.end());
  20. return ret;
  21. }
  22. int main(void){
  23. vector<int> value = {1,4,7,8,11};
  24.  
  25. auto ret = divide(value, [](int a, int b){return abs(b-a)>=2;});
  26.  
  27. for( auto& a : ret ) {
  28. for( auto& b : a )
  29. cout << b << " ";
  30. cout << endl;
  31. }
  32.  
  33. return 0;
  34. }
  35.  
Success #stdin #stdout 0s 15240KB
stdin
Standard input is empty
stdout
1 
4 
7 8 
11