I am studying the book "Accelerated C++" from Koenig & Moo.
Exercise 8-2 ask me to implement on my own some templatized functions from <algorithm>
and <numeric>
, and to specify what kind of iterator does my implementation require.
When trying to implement std::search, I determined that I need only "input" iterators.
Here is my code so far:
template <class In1, class In2>
In1 search(In1 b, In1 e, In2 b2, In2 e2)
{
if (b2 != e2) {
while (b != e) {
if (*b == *b2) {
In1 bc = b;
In2 b2c = b2;
while (bc != e && b2c != e2 && *bc == *b2c) {
++bc;
++b2c;
}
if (b2c == e2)
return b;
}
++b;
}
}
return e;
}
However, looking at the implementation of std::search installed with my compiler, I can see that they use "forward" iterators, but I cannot understand why, because there is no need to write, only to read, and input iterators meet the requirement.
Can anybody here help me to understand this, please? Why would I need to use "forward" iterators to implement std::search?
Thanks in advance.