Skip to main content
caution

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.

C++ version

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​

  1. search for the smallest value ( std::ranges::min())
  2. search for the greatest value ( std::ranges::max())
  3. count how many times a given element occurs ( std::ranges::count())
  4. reverse order ( std::ranges::reverse())
  5. 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:

Numbers array
std::vector<int> numbers = { 22, 13, 27, 4 };

In any case, using the algorithm will come down to typing:

std::ranges::<algorithm_name>( <vector_name> );

// for example:
std::ranges::sort(numbers);

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:

Displaying each number in an array
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.

#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 << ' ';
}
Result
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

Explaination​

The smallest value​

Overview of the 'min' algorithm

To find the smallest value inside a vector, we use std::ranges::min:


std::cout << std::ranges::min(numbers);

The greatest value​

Overview of the 'max' algorithm

To find the greatest value inside a vector, we use std::ranges::max this way:


std::cout << std::ranges::max(numbers);

Counting the occurrences​

note

For the purposes of this algorithm, we're going to add one more value 13 to the end of the array.

Overview of the 'count' algorithm

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):


size_t count = std::ranges::count(numbers, 13);
std::cout << "Occurrences of 13: " << count;

Reversing the order​

Overview of the 'reverse' algorithm

To reverse the order of the vector's elements, we use std::ranges::reverse Β (or std::reverse before C++20).


std::ranges::reverse(numbers);

Sorting​

DziaΕ‚anie algorytmu 'sort'

We use the sort function like this:


std::ranges::sort(numbers);
caution

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.

C++ version

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​

  1. search for the smallest value ( std::ranges::min())
  2. search for the greatest value ( std::ranges::max())
  3. count how many times a given element occurs ( std::ranges::count())
  4. reverse order ( std::ranges::reverse())
  5. 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:

Numbers array
std::vector<int> numbers = { 22, 13, 27, 4 };

In any case, using the algorithm will come down to typing:

std::ranges::<algorithm_name>( <vector_name> );

// for example:
std::ranges::sort(numbers);

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:

Displaying each number in an array
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.

#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 << ' ';
}
Result
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

Explaination​

The smallest value​

Overview of the 'min' algorithm

To find the smallest value inside a vector, we use std::ranges::min:


std::cout << std::ranges::min(numbers);

The greatest value​

Overview of the 'max' algorithm

To find the greatest value inside a vector, we use std::ranges::max this way:


std::cout << std::ranges::max(numbers);

Counting the occurrences​

note

For the purposes of this algorithm, we're going to add one more value 13 to the end of the array.

Overview of the 'count' algorithm

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):


size_t count = std::ranges::count(numbers, 13);
std::cout << "Occurrences of 13: " << count;

Reversing the order​

Overview of the 'reverse' algorithm

To reverse the order of the vector's elements, we use std::ranges::reverse Β (or std::reverse before C++20).


std::ranges::reverse(numbers);

Sorting​

DziaΕ‚anie algorytmu 'sort'

We use the sort function like this:


std::ranges::sort(numbers);