fork download
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <vector>
  4.  
  5. struct Range {
  6. int start;
  7. int end;
  8. };
  9.  
  10. const bool bools[] = {true, false, false, false, true, true, false};
  11. const int n = 7;
  12.  
  13. bool get_bool() {
  14. static int index = 0;
  15. return bools[index++];
  16. }
  17.  
  18. int main() {
  19. std::vector<Range> result;
  20. if (n > 0) {
  21. result.reserve(n);
  22. Range range{0,0};
  23.  
  24. bool curr = get_bool(); // get 1st value
  25. for (int i = 1; i < n; ++i) {
  26. bool val = get_bool(); // get next value
  27. if (val != curr) {
  28. range.end = i - 1;
  29. result.push_back(range);
  30. range.start = i;
  31. }
  32. curr = val;
  33. }
  34.  
  35. range.end = n - 1;
  36. result.push_back(range);
  37. }
  38.  
  39. std::cout << std::boolalpha;
  40. std::cout << '{' << bools[0];
  41. for(int i = 1; i < n; ++i) {
  42. std::cout << ',' << bools[i];
  43. }
  44. std::cout << "}\n";
  45.  
  46. for(const auto &range : result) {
  47. std::cout << range.start << '-' << range.end << '\n';
  48. }
  49.  
  50. return 0;
  51. }
Success #stdin #stdout 0.01s 5520KB
stdin
Standard input is empty
stdout
{true,false,false,false,true,true,false}
0-0
1-3
4-5
6-6