Why the std::search(b, e, b2, e2) use Forward Iterator?

L

Lambda

Hi, All,

Why the search() function use Forward Iterator?
I think Input Iterator is enough, I need read only.

In such kind of situations, how should I choose iterator type?
 
K

Kai-Uwe Bux

Lambda said:
Hi, All,

Why the search() function use Forward Iterator?
I think Input Iterator is enough, I need read only.

In such kind of situations, how should I choose iterator type?

Please refer to clause [24.1.1/3]:

[Note: For input iterators, a == b does not imply ++a == ++b. (Equality
does not guarantee the substitution property or referential transparency.)
Algorithms on input iterators should never attempt to pass through the
same iterator twice. They should be single pass algorithms. Value type T
is not required to be an Assignable type (23.1). These algorithms can be
used with istreams as the source of the input data through the
istream_iterator class. ]

Note that std::search might want to back-track. That is why the standard
required forward iterators not only on the pattern.


Best

Kai-Uwe Bux
 
A

alan

Lambda said:
Hi, All,

Why the search() function use Forward Iterator?
I think Input Iterator is enough, I need read only.
So that an implementation can use a multipass search algorithm on a
container. Input Iterators will "lose" the previous position
completely once the position is advanced (think I/O input streams),
but you can have two separate instances of a Forward Iterator, with
one delayed from the other (think vectors).
In such kind of situations, how should I choose iterator type?
//if you need read only, otherwise use ::iterator
typename std::insert_standard_container_type_here::const_iterator
iter;
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top