Note, this article is not finished! You can help by editing this doc page.
Using algorithms
In the C++ standard library we find a lot of ready-made algorithms that we can use to manipulate vectors.
We recommend that you use the C++ 20 version as the solutions are simpler. For people who, for some reason, cannot install a compiler that supports the latest standard, we will also include examples that work on an older version.
To be able to use the algorithms, we need to add the following header to the code:
#include <algorithm>
Motivationβ
- search for the smallest value ( std::ranges::min())
- search for the greatest value ( std::ranges::max())
- count how many times a given element occurs ( std::ranges::count())
- reverse order ( std::ranges::reverse())
- sort vector elements ( std::ranges::sort())
Algorithmsβ
At the beginning, let's create a simple program on which we will test the operation of the algorithms. Let's prepare an array of numbers:
std::vector<int> numbers = { 22, 13, 27, 4 };
In any case, using the algorithm will come down to typing:
- C++20
- until C++20
std::ranges::<algorithm_name>( <vector_name> );
// for example:
std::ranges::sort(numbers);
std::<algorithm_name>(<vector_name>.begin(), <vector_name>.end());
// for example:
std::sort(numbers.begin(), numbers.end());
where instead of <vector_name>
we insert the prepared above numbers
.
We will display its content after each operation using the for
loop we learned earlier:
for (int n : numbers)
std::cout << n << ' ';
Demonstrationβ
First, let's see an example code using all these algorithms, and then we'll discuss them.
- C++20
- until C++20
#include <iostream>
#include <vector>
#include <algorithm>
int main() {
std::vector<int> numbers = { 22, 13, 27, 4 };
int min = std::ranges::min(numbers);
int max = std::ranges::max(numbers);
std::cout << "The smallest: " << min << '\n';
std::cout << "The greatest: " << max << '\n';
// For the sake of demonstration of the "count"
// function I add a single 13 number to the end:
numbers.push_back(13);
size_t count = std::ranges::count(numbers, 13);
std::cout << "Occurrences of 13: " << count << '\n';
std::cout << "Before reversing:\n";
for (int n : numbers)
std::cout << n << ' ';
std::ranges::reverse(numbers);
std::cout << "\nAfter reversing:\n";
for (int n : numbers)
std::cout << n << ' ';
std::cout << "\nBefore sorting:\n";
for (int n : numbers)
std::cout << n << ' ';
std::ranges::sort(numbers);
std::cout << "\nAfter sorting:\n";
for (int n : numbers)
std::cout << n << ' ';
}
The smallest: 4
The greatest: 27
Occurrences of 13: 2
Before reversing:
22 13 27 4 13
After reversing:
13 4 27 13 22
Before sorting:
13 4 27 13 22
After sorting:
4 13 13 22 27
#include <iostream>
#include <vector>
#include <algorithm>
int main() {
std::vector<int> numbers = { 22, 13, 27, 4 };
int min = *std::min_element(numbers.begin(), numbers.end()); // π‘
int max = *std::max_element(numbers.begin(), numbers.end()); // π‘
std::cout << "The smallest: " << min << '\n';
std::cout << "The greatest: " << max << '\n';
// For the sake of demonstration of the "count"
// function I add a single 13 number to the end:
numbers.push_back(13);
size_t count = std::count(numbers.begin(), numbers.end(), 13);
std::cout << "Occurrences of 13: " << count << '\n';
std::cout << "Before reversing:\n";
for (int n : numbers)
std::cout << n << ' ';
std::reverse(numbers.begin(), numbers.end());
std::cout << "\nAfter reversing:\n";
for (int n : numbers)
std::cout << n << ' ';
std::cout << "\nBefore sorting:\n";
for (int n : numbers)
std::cout << n << ' ';
std::sort(numbers.begin(), numbers.end());
std::cout << "\nAfter sorting:\n";
for (int n : numbers)
std::cout << n << ' ';
}
The smallest: 4
The greatest: 27
Occurrences of 13: 2
Before reversing:
22 13 27 4 13
After reversing:
13 4 27 13 22
Before sorting:
13 4 27 13 22
After sorting:
4 13 13 22 27
The code marked with π‘ is more complicated and unfortunately in versions before C++20 there is no other, simpler possibility. If you are using an older version of C++, consider installing a compiler that supports the latest language standard.
Explainationβ
The smallest valueβ
To find the smallest value inside a vector, we use std::ranges::min
:
- C++20
- until C++20
std::cout << std::ranges::min(numbers);
std::cout << std::min(numbers.begin(), numbers.end());
The greatest valueβ
To find the greatest value inside a vector, we use std::ranges::max
this way:
- C++20
- until C++20
std::cout << std::ranges::max(numbers);
std::cout << std::max(numbers.begin(), numbers.end());
Counting the occurrencesβ
For the purposes of this algorithm, we're going to add one more value 13
to the end of the array.
count
is used to count the occurrences of elements, in this case, inside the vector. We use it like this:
We can also write this result to a variable. We should use a special type size_t
for this,
which is simply a non-negative number (because the number of occurrences cannot be negative):
- C++20
- until C++20
size_t count = std::ranges::count(numbers, 13);
std::cout << "Occurrences of 13: " << count;
size_t count = std::count(numbers.begin(), numbers.end(), 13);
std::cout << "Occurrences of 13: " << count;
Reversing the orderβ
To reverse the order of the vector's elements, we use std::ranges::reverse
Β (or std::reverse
before C++20).
- C++20
- until C++20
std::ranges::reverse(numbers);
std::reverse(numbers.begin(), numbers.end());
Sortingβ
We use the sort
function like this:
- C++20
- until C++20
std::ranges::sort(numbers);
std::sort(numbers.begin(), numbers.end());