The STL provides generic algorithms that operate on iterators—sort, find, count, transform—decoupled from container types.
Common algorithms
std::sort(v.begin(), v.end());
auto it = std::find(v.begin(), v.end(), 42);
int n = std::count_if(v.begin(), v.end(), pred);
Combine algorithms with lambdas for expressive data processing without hand-rolled loops everywhere.
Important interview questions and answers
- Q: Algorithm requirements?
A: Valid iterator range [begin, end) with appropriate category (forward, random access, etc.). - Q: sort complexity?
A: Typically O(n log n)—Introsort in standard library implementations.
Self-check
- What do algorithms take instead of containers directly?
- Name two STL algorithms.
Tip: Algorithms + lambdas often replace hand-written loops—read <algorithm> before reinventing sort/find.
Interview prep
- Algorithms take what?
Iterator ranges [begin, end)—decoupled from specific container types.
- std::sort complexity?
Typically O(n log n) in standard library implementations.