I'm studying "Quick C ++" from Koenig; Moo.
Exercise 8-2 gives me some templatized functions of my own & lt; Algorithm & gt;
and & lt; Numerical & gt;
to implement, and specify which type of Iterator requires my implementation.
When trying to implement std :: search
, I determined that I only "input" iterator
this is my code now Is:
template & lt; Class In1, class In2 & gt; These 1 search (in 1b, in 1e, in 2b2, in 2 e2) {if (b2! = E2) {while (b! = E) {if (* b == * b2) {In1cc = b; In 2b 2c = b2; While (BC! = E & amp; B 2C! = E 2 & amp; BC == * B2C) {++ BC; ++ B2C; } If (B2C == E2) returns B; } ++ b; }} Return e; }
However, by looking at the implementation of std :: search
with my compiler, I can see that they use "forward" regenerators , But I do not understand why, because there is no need to write, only to read, and the input itater meets the requirements.
Can someone help me understand here, please? Why use "forward" regenerators to implement std :: search
With input iterator, you are allowed to go through the range only once, that is, once you have been redirected and increased the itater, you can not go back and repeat it again Are there.
This class of itater is very useful for many things. For example, if you are reading with some type of stream, once you've read something from the stream, you can not go back and read that thing again; You can not store a copy of the itater, continue with the original errator, then go back and start again with the copy and assume that you will get the same result.
In your algorithm, you go through more than once (see comments in your source here):
template & lt; Class In1, class In2 & gt; These 1 search (in 1b, in 1e, in 2b2, in 2 e2) {if (b2! = E2) {while (b! = E) {if (* b == * b2) {In1cc = b; // copy iterator b in 2b2c = b2; While (BC! = E & amp; B 2C! = E 2 & amp; BC == * B2C) {++ BC; // Increasing copy of Iterator B ++ B2C; } If (B2C == E2) returns B; } ++ b; // Basic B Increase]} Return e; }
Forward Iterator
is the most basic type of Iterator, which can be used for multiple algorithms, because it guarantees that once again Can repeat many times. / P>
Whether an iterator is unstable or irreversible (i.e., you can modify that element, for which it is a reference of an Iterator) is independent of the category of the Iterator: a ForwardIterator
can be irreversible, for example, the constructors of any Iterator class are irreversible (or, for a solid example, std :: forward_list & lt; int & gt; :: const_iterator
c + An unchanging forwarding in + 0x).
Comments
Post a Comment