Finding all intervals that match predicate in vector I have a function find_all_intervals_below that iterates through a vector and finds all the index intervals of at least a given length where each element within the interval is below a given threshold.

struct Interval {
    int start;
    int end;
};

std::vector&lt;Interval&gt;
find_all_intervals_below(const std::vector&lt;int&gt; &amp;v, const int &amp;threshold,
                         const int &amp;min_length) {
    auto start_position { 0 };
    auto end_position { 0 };
    std::vector&lt;Interval&gt; intervals;
    bool found_start { false };

    for (auto current_pos = 0; current_pos &lt; v.size(); ++current_pos) {
        if (v[current_pos] &lt;= threshold and not found_start) {
            start_position = current_pos;
            end_position = 0;
            found_start = true;
        } else if (found_start and v[current_pos] &gt; threshold and end_position == 0) {
            end_position = current_pos;
            if (end_position - start_position &gt;= min_length) {
                Interval interval;
                interval.start = start_position;
                interval.end = end_position;
                intervals.push_back(interval);
            }
            start_position = 0;
            found_start = false;
        }
    }
    if (found_start and end_position == 0 and v.size() - start_position &gt;= min_length) {
        end_position = v.size();
        Interval interval;
        interval.start = start_position;
        interval.end = end_position;
        intervals.push_back(interval);
    }
    return intervals;
}


This function works perfectly fine, I would just like to get some input from others as I imagine there is likely much more succinct ways of doing this. search_n from STL looks like it might be a solution but I couldn't figure out how to use it for my case.

EDIT: I need the solution to be C++11 compatible, unfortunately.

Test example

const auto min_len { 3 };
const auto threshold { 3 };
const std::vector&lt;int&gt; v { 4, 2, 1, 1, 4, 1, 2, 4 };

const auto actual { find_all_intervals_below(v, threshold, min_len) };
const std::vector&lt;Interval&gt; expected { Interval(1, 4) };

assert(actual == expected);
Code Review


This piece

        Interval interval;
        interval.start = start_position;
        interval.end = end_position;
        intervals.push_back(interval);


can be transformed into

intervals.emplace_back(start_position, end_position);

Don't accept small objects by reference for read-only purposes. Although it usually doesn't hurt, in most implementations reference (which is implemented as pointer) will take up more space (compiler will probably inline the function or just pass by value though).
Algorithm. When there is a state which is represented by combination of flags and some metadata, flags usually go out of hand quickly. I would instead implement something like this:

1. Set previous, current to start of the input
2. previous = current
3. current = first index of element that is higher than threshold
4. if current - previous &gt;= minlength, add to result
5. increment current
6. Go to 2


One could also create it the other way around, e.g. searching for those below threshold.


Alternative implementation

#include &lt;vector&gt;
#include &lt;algorithm&gt;
#include &lt;type_traits&gt;

using index_type = std::make_signed_t&lt;std::size_t&gt;;

struct interval {
    index_type first;
    index_type last;
};

bool operator==(const interval lhs, const interval rhs) {
    return lhs.first == rhs.first &amp;&amp; lhs.last == rhs.last;
}

std::vector&lt;interval&gt; find_suitable_intervals(const std::vector&lt;int&gt;&amp; input, 
                                              const int threshold, 
                                              const index_type min_length) {
    auto predicate = [threshold](int x) {
        return x &lt;= threshold;
    };
    std::vector&lt;interval&gt; intervals;
    auto first = input.begin();
    auto previous = input.begin();
    auto current = first;
    while (current != input.end()) {
        previous = current;
        current = std::find_if_not(current, input.end(), predicate);
        if (current - previous &gt;= min_length) {
            intervals.push_back({previous - first, current - first});
        }
        if (current == input.end()) {
            break;
        }
        ++current;
    }

    return intervals;
}

int main() {
    const int min_length = 3;
    const int threshold = 3;
    const std::vector&lt;int&gt; v { 4, 2, 1, 1, 4, 1, 2, 4 };

    const auto actual = find_suitable_intervals(v, threshold, min_length);
    const std::vector&lt;interval&gt; expected { {1, 4} };

    return actual != expected;
}


Wandbox Demo.

The logic got more "flat", but there are culprits of bridging STL style with more traditional style. Also, since incrementing iterator beyond end will cause undefined behavior, I had to put in the condition to check if the loop reached end. Mixing std::size_t and std::distance/difference will cause a warning and will require a cast to get rid of the warning, since one is unsigned and the other is not, thus I created index_type. There are rumors of std::index, but I wouldn't expect it in near future.

One could also make the condition an input into the function, e.g. predicate. Then it would look like this:

find_suitable_intervals(data, min_length, [threshold](auto x) { 
    x &lt; threshold;
});


Which is I believe is a bit more readable.