STL algorithms and const_iterators
c++, constants, find, iterator, stl
Solution
`std::find` and `std::find_if` can definitely operate on `*::const_iterator` for a given container. Are you by chance looking at the signatures of those functions, and misunderstanding them?
template <class InputIterator, class Type>
InputIterator find(InputIterator first, InputIterator last, const Type& val);
Note that `InputIterator` here is just a name of a template type parameter, and any `const_iterator` will satisfy the requirements for it.
Or, perhaps, you're confusing `const_iterator` (i.e. an iterator referencing a const value) with a `const` iterator (i.e. an iterator which is itself `const`)?
Problem
Today I wrote a small predicate to find matching symbols in a container. But I'm faced to a problem: I want to use this predicate in a `std::find_if` call inside a const-method of a class, searching in a container that is a member of this class. But I just noticed that neither `std::find` nor `std::find_if` are able to operate on `const_iterators` ! I checked on some C++ references and it seems there is no version of `std::find` or `std::find_if` that accept/return `const_iterators`. I just can't understand why, since from what I've seen, there is no way that these algorithms could modify the object referenced by the iterator. Here is how is documented `std::find` in the SGI implementation: Returns the first iterator i in the range [first, last) such that *i == value. Returns last if no such iterator exists.