Using stdfind With Reverse Iterators
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
std::find works with reverse iterators just as it does with normal iterators, as long as the iterator category requirements are met. The main thing that confuses people is not the search itself, but how to interpret the returned iterator and how to convert it back to a forward iterator correctly.
Searching from the End
If you want to find the last matching value in a container, reverse iterators are often the easiest approach.
This finds the first 4 in reverse traversal, which corresponds to the last 4 in the normal forward view of the vector.
What the Returned Reverse Iterator Means
When std::find returns a reverse iterator:
- dereferencing it gives the matched element
- incrementing it moves backward through the underlying container
- comparing it against
rend()tells you whether a match was found
This part is straightforward. The subtle part comes when you want the equivalent normal iterator.
Converting with .base()
For a reverse iterator rit, rit.base() does not point to the same element. It points to the forward iterator one position after the element viewed by the reverse iterator.
That surprises many people the first time they see it.
Getting the Forward Iterator to the Same Element
If you need a normal iterator to the matched element itself, move one step back from base().
That std::prev adjustment is the crucial conversion step.
Why Reverse Iterators Are Useful
Using reverse iterators is often cleaner than:
- writing a manual reverse loop
- scanning from the front and remembering the last match
- subtracting indexes manually
For containers such as std::vector, std::string, and std::deque, reverse iterators make "find the last matching item" read naturally.
Example with std::string
This is a very readable pattern for last-occurrence searches.
Prefer Dedicated Algorithms When They Exist
For some containers or string types, there may be a more direct function such as rfind. If the type already gives you the operation you want, use that first. Reverse iterators are most useful when you want a generic STL algorithm or need logic that goes beyond a built-in convenience method.
Common Pitfalls
The biggest mistake is assuming rit.base() points to the found element. It actually points one position past it in forward-iterator terms.
Another issue is dereferencing rit.base() without checking what it means semantically. That often yields the next element instead of the matched one.
A third problem is forgetting to compare against rend() when searching in reverse. rend() is the "not found" sentinel for reverse traversal.
Summary
- '
std::findworks normally with reverse iterators.' - Searching with
rbegin()andrend()is a clean way to find the last matching element. - A reverse iterator's
.base()points one element past the reverse view's current element. - Use
std::prev(rit.base())when you need the forward iterator to the matched item. - Prefer container-specific helpers such as
rfindwhen they already solve the problem directly.
Related reading
- Virtual member call in a constructor
- Visual studio long compilation when replacing int with double
- What is the '-->' operator in C/C++?
- What algorithm is behind STL's find?
- What algorithms are used in C11 stdsort in different STL implementations?
- What algorithms do popular C compilers use for stdsort and stdstable_sort?
- What are C++ functors and their uses?
- What are POD types in C++?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.