Intermediate · The Standard Template Library (STL) · topic 25 of 42 · 35 min
STL algorithms: sort, find, count, accumulate and binary search
Explanation
Why use algorithms?
You could write your own loop to find the maximum mark or sort a list. But hand-written loops are where off-by-one bugs live, and a reader must study each loop to see what it does. std::max_element(v.begin(), v.end()) says what it does in its name and has been tested by millions of programs. The C++ Core Guidelines advise: prefer standard algorithms to raw loops.
Sorting
#include <algorithm>
#include <functional>
#include <vector>
std::vector<int> marks = {72, 95, 60, 88};
std::sort(marks.begin(), marks.end()); // 60 72 88 95
std::sort(marks.begin(), marks.end(), std::greater<int>()); // 95 88 72 60
struct Student { std::string name; int marks; };
std::vector<Student> cls = {{"Asha", 88}, {"Ravi", 95}, {"Meena", 88}};
std::sort(cls.begin(), cls.end(), [](const Student& a, const Student& b) {
if (a.marks != b.marks) return a.marks > b.marks; // higher marks first
return a.name < b.name; // then by name
});
// Ravi 95, Asha 88, Meena 88std::sort is not stable: equal elements may change their relative order. If you need equal elements to keep their original order (for example sorting by marks while keeping roll-number order for ties), use std::stable_sort.
Searching and counting
#include <algorithm>
#include <numeric>
std::vector<int> v = {4, 8, 15, 16, 23, 42};
auto it = std::find(v.begin(), v.end(), 16); // iterator to 16, or v.end()
int evens = std::count_if(v.begin(), v.end(), [](int x) { return x % 2 == 0; }); // 4
auto [mn, mx] = std::minmax_element(v.begin(), v.end()); // *mn 4, *mx 42
long long total = std::accumulate(v.begin(), v.end(), 0LL); // 108
bool anyBig = std::any_of(v.begin(), v.end(), [](int x) { return x > 40; }); // trueBinary search on sorted data
std::vector<int> sorted = {10, 20, 20, 20, 30, 40};
bool has = std::binary_search(sorted.begin(), sorted.end(), 30); // true
auto lo = std::lower_bound(sorted.begin(), sorted.end(), 20); // first element >= 20 (index 1)
auto hi = std::upper_bound(sorted.begin(), sorted.end(), 20); // first element > 20 (index 4)
std::cout << hi - lo << '\n'; // 3 copies of 20Rearranging
- std::reverse(v.begin(), v.end()) reverses in place.
- std::unique removes consecutive duplicates (sort first to remove all duplicates), returning the new end; follow it with erase.
- std::rotate, std::partition and std::nth_element (find the k-th smallest in O(n) on average) are worth knowing for interviews.
- std::next_permutation steps through all orderings of a sorted range, handy for brute-force contest problems.
- std::fill and std::iota (from <numeric>) fill a range with one value or with 0, 1, 2, ...
std::vector<int> ids = {5, 3, 5, 1, 3};
std::sort(ids.begin(), ids.end()); // 1 3 3 5 5
ids.erase(std::unique(ids.begin(), ids.end()), ids.end()); // 1 3 5Complexity summary
| Algorithm | Time |
|---|---|
| sort, stable_sort | O(n log n) |
| find, count, accumulate, min/max_element, reverse, unique | O(n) |
| binary_search, lower_bound, upper_bound (sorted, random access) | O(log n) |
| nth_element | O(n) on average |
Need help?
Stuck on a word or an idea? Search this course, or every course, and jump straight to the explanation or a worked question.
Try:
Watch the lesson
STL algorithms: sort, find, count, accumulate and binary search · 1/6
The STL algorithms are tested, fast functions for common jobs. Prefer them to hand-written loops.
Prefer a video by a teacher? Find free videos on “c++ stl algorithms sort lower_bound accumulate lambda comparator tutorial” on YouTube ↗
In real life: A college merit list
For admissions, a college sorts applicants by entrance score (highest first), breaks ties by date of birth (older first), and then uses lower_bound on the sorted cut-off list to tell each applicant which category cut-off they cleared. All of it is a few lines of STL.
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
struct Applicant { std::string name; int score; int birthYear; };
int main() {
std::vector<Applicant> list = {{"Asha", 182, 2006}, {"Ravi", 190, 2007}, {"Kiran", 182, 2005}};
std::sort(list.begin(), list.end(), [](const Applicant& a, const Applicant& b) {
if (a.score != b.score) return a.score > b.score;
return a.birthYear < b.birthYear; // older first on a tie
});
for (const auto& a : list) std::cout << a.name << ' ' << a.score << '\n';
}
// Ravi 190
// Kiran 182
// Asha 182Practice (0/4 done)
std::vector<int> v = {3, 1, 2}; std::sort(v.begin(), v.end()); What is v?
What does this print? std::vector<int> v = {1, 3, 3, 3, 7}; auto a = std::lower_bound(v.begin(), v.end(), 3); auto b = std::upper_bound(v.begin(), v.end(), 3); std::cout << (a - v.begin()) << " " << (b - a);
std::vector<double> p = {1.5, 2.5}; What is std::accumulate(p.begin(), p.end(), 0)?
Read n and then n prices. Answer q queries: for each budget b, print how many items cost at most b. Sort once, then use std::upper_bound for each query, so the whole program is O((n + q) log n).
Quick quiz
Read more (free)
Related help
Before this, learn
This topic builds on these. If something here feels new, open them first.
Related topics
- std::map and std::unordered_mapThe Standard Template Library (STL) · same module
- Two-dimensional arrays and array algorithmsJava Programming (Core to Spring Boot) · Arrays and strings
- 2D arrays and classic array algorithmsC Programming (from zero to systems) · Functions and arrays
- Binary searchData Structures and Algorithms (DSA) for interviews · Sorting and searching
- Lambdas: small functions written in placeModern C++: lambdas, C++17 and C++20 essentials
- Big-O and searchingPython Programming · Algorithms, data structures and testing
Questions people ask about this
Open a question to see the answer and why. The first ones are this topic's own quiz, so try the quiz above before you peek.
What must be true before using std::binary_search or std::lower_bound?
Answer: The range is sorted (with the same ordering)
Binary search relies on sorted order to throw away halves.
Which algorithm keeps equal elements in their original order?
Answer: std::stable_sort
stable_sort is stable; sort is not guaranteed to be.
What is the time complexity of std::sort?
Answer: O(n log n)
The standard requires O(n log n) comparisons.
What does std::unique do on {1, 1, 2, 1}?
Answer: Removes only consecutive duplicates, giving {1, 2, 1} at the front and returns the new end
unique only collapses runs of equal neighbours; sort first to remove all duplicates.
Why is [](int a, int b) { return a <= b; } a bad comparator?
Answer: It is not a strict weak ordering, so std::sort may misbehave or crash
For equal elements it says each comes before the other, which breaks sort's assumptions.
Why can you not call std::sort on a std::list?Related
Answer: std::sort needs random-access iterators; list iterators are only bidirectional
Use the member function list.sort() instead.
What must be true for binary search to work?Related
Answer: The array is sorted
Binary search relies on sorted order.
Which must be true for binary search?Related
At most how many steps does binary search need on 1,000,000 sorted items?Related
Pass the quiz (60% or more) or mark this topic as done when you are ready.
Your progress is saved on this device only.
Next steps
- This is the last topic of “The Standard Template Library (STL)”. The module exam comes next.
- Module 6 exam: The STL · 13 questions · 25 min, without help. 5 topics of this module still to finish first.
- Then the next module, Templates, smart pointers and move semantics. 42 topics left in the course before the final exam.
← Previous: Iterators: the glue between containers and algorithms