C++ Algorithms

Beginner
8 min read
Updated: Nov 2026
4 examples
<algorithm>

Definition and Usage

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.

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

Core Ideas

Five ideas that appear in almost every algorithm guide:

IdeaExampleRemember
Iteratorv.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
Comparatorstd::greater<int>()Decides which of two elements comes first
Inserterstd::back_inserter(out)Lets a copy grow a vector with push_back()

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;
}

How It Works

  1. #include <algorithm> brings in sort(), find() and count().
  2. 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.
  3. 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;
}

How It Works

  1. The lambda [](int n) { return n > 20; } is a predicate. It answers true or false for one number.
  2. count_if() calls it for 10, 25, 40 and 5.
  3. 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;
}

How It Works

  1. reverse() flips the vector in place, so 4 9 2 becomes 2 9 4.
  2. max_element() does not change anything. It returns an iterator to the biggest value.
  3. *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;
}

How It Works

  1. v.begin() + 1 is the first element to sort, and v.begin() + 4 is the first one to leave alone.
  2. Only 8, 7 and 6 are inside that range, so only they are sorted.
  3. The 9 at the front and the 5 at the back stay where they were.

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:

Start withWhy
sort()Put data in order
find()Find a value and get its position
count()Count how often a value appears
count_if()Count with a condition
reverse()Flip the order
max_element()Find the largest value
binary_search()Fast search in sorted data

Full list: C++ Algorithm Functions index (/cpp/algorithm/function).

Frequently Asked Questions

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().

Browse every algorithm

Open the functions index for categorized guides with examples and FAQs.

Function index →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

18 people found this page helpful