In C++, <algorithm> is a standard header full of ready-made functions for common jobs: sorting, searching, counting, copying, reversing, and more. You do not need to write the loops yourself.
Most algorithms work on a range of elements. You describe the range with two iterators. v.begin() points at the first element and v.end() points just after the last one. The same algorithm then works on a std::vector, an array, or a std::string.
This page teaches the basic ideas. The Algorithm Functions index lists every function guide in this series.
Use the name, not a loop.
Code like std::sort(v.begin(), v.end()) tells the reader exactly what happens. It is also well tested and fast.
One header.
#include <algorithm> gives you sort, find, count, reverse and the rest. Add <vector> for the container.
Syntax
C++ Syntax
Every algorithm call follows the same pattern:
C++
#include <algorithm>
#include <vector>
std::vector<int> v = {5, 2, 9, 1};
std::sort(v.begin(), v.end()); // range, no extra input
std::find(v.begin(), v.end(), 9); // range + a value
std::count_if(v.begin(), v.end(),
[](int n) { return n > 3; }); // range + a condition
std::sort(v.begin(), v.end(), std::greater<int>()); // range + a rule
Reference
Core Ideas
Five ideas that appear in almost every algorithm guide:
Idea
Example
Remember
Iterator
v.begin(), v.end()
Points at an element. end() is just after the last one
Range
[first, last)
Includes first, stops before last
Predicate
[](int n) { return n > 10; }
Answers true or false for one element
Comparator
std::greater<int>()
Decides which of two elements comes first
Inserter
std::back_inserter(out)
Lets a copy grow a vector with push_back()
Code
C++ Examples
Four short programs that show how algorithms are used in a C++ tutorial.
Example 1 — Starter: sort, find, count
Three of the most useful algorithms in one small program.
C++
#include <algorithm>
#include <iostream>
#include <vector>
int main() {
std::vector<int> v = {8, 3, 5, 1, 3};
std::sort(v.begin(), v.end());
std::cout << "Sorted: ";
for (int x : v) std::cout << x << ' ';
std::cout << '\n';
auto it = std::find(v.begin(), v.end(), 5);
std::cout << "5 is at index " << (it - v.begin()) << '\n';
int threes = std::count(v.begin(), v.end(), 3);
std::cout << "3 appears " << threes << " times\n";
return 0;
}
Output
Sorted: 1 3 3 5 8
5 is at index 3
3 appears 2 times
How It Works
#include <algorithm> brings in sort(), find() and count().
sort() puts the vector in order, so 5 ends up at index 3. find() returns an iterator and it - v.begin() turns it into an index.
count() returns a plain number, here the two 3s that sit next to each other after sorting.
Example 2 — A condition with count_if
Count with a rule instead of a fixed value.
C++
#include <algorithm>
#include <iostream>
#include <vector>
int main() {
std::vector<int> v = {10, 25, 40, 5};
int big = std::count_if(v.begin(), v.end(), [](int n) { return n > 20; });
std::cout << "Numbers above 20: " << big << '\n';
return 0;
}
Output
Numbers above 20: 2
How It Works
The lambda [](int n) { return n > 20; } is a predicate. It answers true or false for one number.
count_if() calls it for 10, 25, 40 and 5.
25 and 40 pass, so the result is 2.
Example 3 — reverse and max_element
One function changes the vector, the other only looks at it.
C++
#include <algorithm>
#include <iostream>
#include <vector>
int main() {
std::vector<int> v = {4, 9, 2};
std::reverse(v.begin(), v.end());
std::cout << "Reversed: ";
for (int x : v) std::cout << x << ' ';
std::cout << '\n';
auto it = std::max_element(v.begin(), v.end());
std::cout << "Largest: " << *it << '\n';
return 0;
}
Output
Reversed: 2 9 4
Largest: 9
How It Works
reverse() flips the vector in place, so 4 9 2 becomes 2 9 4.
max_element() does not change anything. It returns an iterator to the biggest value.
*it reads the value that the iterator points at.
Example 4 — Work on part of a vector
The two iterators can mark a smaller range.
C++
#include <algorithm>
#include <iostream>
#include <vector>
int main() {
std::vector<int> v = {9, 8, 7, 6, 5};
std::sort(v.begin() + 1, v.begin() + 4);
for (int x : v) std::cout << x << ' ';
std::cout << '\n';
return 0;
}
Output
9 6 7 8 5
How It Works
v.begin() + 1 is the first element to sort, and v.begin() + 4 is the first one to leave alone.
Only 8, 7 and 6 are inside that range, so only they are sorted.
The 9 at the front and the 5 at the back stay where they were.
Continue
Next: C++ Algorithm Functions Index
After you know what an algorithm is, open the function index for full tutorials on all 91 functions. These are good first stops:
It is a standard header with ready-made functions for common jobs, such as sorting, searching, counting, copying, and reversing. You include it with #include <algorithm>.
An iterator points at one element in a container. v.begin() points at the first element and v.end() points just after the last one. Algorithms use two iterators to know which elements to work on.
Use an algorithm when one exists for your job. Names like std::sort() and std::count() explain what the code does, and they are well tested and fast.
A predicate is a small function that answers true or false for one element. A lambda is a short way to write one inline, for example [](int n) { return n > 10; }.
Practice the examples on this page, then open the Algorithm Functions index. Start with sort(), find(), count(), and reverse().