Finding all intervals that match predicate in vector
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.