What algorithm is behind STL's find?
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
std::find is not powered by a hidden tree, hash table, or binary search. At the specification level, it is a linear search over the iterator range, which means it checks elements one by one until it finds a match or reaches the end.
The Conceptual Algorithm Is Simple
The mental model for std::find is almost exactly this:
That is the essential algorithm. It starts at first, compares each element to value, and returns the iterator to the first match.
Why the Complexity Is Linear
Because std::find may need to inspect each element in the worst case, its complexity is linear in the number of elements in the range.
That gives you:
- Best case: the first element matches.
- Worst case: no element matches, or the match is last.
- Complexity guarantee: proportional to the range length.
This is exactly what you should expect from a general-purpose search algorithm that works with input iterators and does not assume sorting or indexing.
std::find Is General, Not Magical
The strength of std::find is generality:
- It works with many container types.
- It works with iterator ranges instead of requiring a specific container API.
- It relies only on equality comparison and iterator traversal.
That generality is why the algorithm is simple. The standard library cannot assume the data is sorted, contiguous, or indexed.
Example:
What It Uses for Comparison
std::find uses equality comparison, conceptually *it == value. That means your type must support the relevant equality operation for the algorithm to work as expected.
For custom types:
If equality is defined poorly, std::find will faithfully follow that poor definition.
Why It Is Not Binary Search
People sometimes expect std::find to be clever when the container is sorted. It is not. If the data is sorted and you want logarithmic lookup, use the appropriate algorithm such as std::binary_search or std::lower_bound.
std::find does not inspect container ordering at all. It just walks the range.
That distinction matters because using std::find on a large sorted vector wastes information the algorithm is not designed to use.
Implementation Optimizations Can Exist
Library implementations may optimize specific cases internally, especially for simple contiguous data types, but those are implementation details. The semantic model and complexity guarantee are still linear search.
That is the right way to think about it:
- Specification view: linear search.
- Implementation view: maybe optimized linear search in special cases.
You should program against the specification, not against hoped-for library tricks.
Common Pitfalls
- Assuming
std::findbecomes logarithmic just because the container is sorted. - Forgetting that it returns
lastwhen nothing matches. - Defining
operator==incorrectly for custom types. - Using
std::findwhen a container-specific lookup such asmap::findwould be more appropriate. - Treating STL algorithm names as if they imply more intelligence than the specification actually guarantees.
Summary
- '
std::findis conceptually a linear search over an iterator range.' - It checks elements one by one using equality comparison.
- Its generality is exactly why the algorithm is simple.
- Sorted data does not make
std::finda binary search. - Choose a more specialized algorithm or container lookup when the data structure offers stronger guarantees.
Related reading
- What algorithm is this? Best way to distribute limited resources
- What algorithm should I use for high-performance large integer division?
- What algorithm to use to determine minimum number of actions required to get the system to Zero state?
- What algorithm to use to segment a sequence of numbers into n subsets, to minimize the standard deviation of the sum of the numbers in each subset
- What algorithms are used in C11 stdsort in different STL implementations?
- What algorithms do popular C compilers use for stdsort and stdstable_sort?
- What algorithm used to find the nth sorted subarray of an unordered array?
- What algorithms are suitable for this simple machine learning problem?

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.