Suppose we have two ranges r1 = [first1, last1) and r2 = [first2, last2)
Let’s say that r2 is a subrange of r1 iff there exists such i>=0 that
-
[first1 + i, first1 + i + last2 - first2)is a valid range -
for all j in
[0, last2 - first2)the following holds:*(first1 + i + j) == *(first2 + j)
A nested loop can easily determine if r2 is a subrange of r1, or even one loop with a nested call to std::equal template. Is there a more STL-ish, concise way to express the same idea in C++? C++0x solutions are welcome, too. Thanks in advance.
I think
std::searchis what you’re looking for. From http://www.cplusplus.com/reference/algorithm/search/ [std::search] “Searches the range [first1,last1) for the first occurrence of the sequence defined by [first2,last2), and returns an iterator to its first element.”