The C++17 standard came with a lot of new features and improvements. One of the features was the math.h
library which modernized math operations with the cmath library. The Parallelism Technical Specification adds several new algorithms to the standard C++ library that we can use in C++ Builder 12. These are modernized in the <algorithm>
header in the standard library. In this post, we explain the new algorithms that come with C++17.
Table of Contents
What Are The New Algorithms That Come With C++ 17?
C++17 is mostly focused on efficient parallel execution. The new algorithms which support that are available in the usual simple form as well: for_each_n
, sample
, search
, reduce
, transform_reduce
, exclusive_scan
, inclusive_scan
, transform_exclusive_scan
, transform_inclusive_scan
. Now, let’s see some of examples these new algorithms.
Iterating ( std::for_each_n )
In C++ 14, we had for_each()
to process each element in a given range. In C++17, there is a new std::for_each_n
algorithm that executes a function object for each element in the range.
1 2 3 |
std::for_each_n( first_elemnt, number_of_elements, function) |
1 2 3 4 5 6 7 8 9 |
std::vector<int> vec {47, 12, 3, 9, 5, 21}; for_each_n( vec.begin(), 3, [](const auto& param) { std::cout << param << std::endl; } ); |
Sampling ( std::sample )
The std:: sample
routine is defined in <algoritm>
header that selects elements from a given range and gives output those selected elements into the output iterator and random numbers are generated using the random number generator.
We can simply write this syntax for it:
1 2 3 |
std::sample( first_data, last_data, output_container, number_of_elements, random_generator); |
Here is an example that generates sample from vec
to vec2
.
1 2 3 4 5 6 7 |
std::vector<int> vec {47, 12, 3, 9, 5, 21}; std::vector<int> vec2 {0, 0, 0}; std::sample( vec.begin(), vec.end(), std::back_inserter(vec2), 12, std::mt19937 {std::random_device{}()}); |
Searching ( std::search )
The std::search
is defined in <algorithm>
header in standard library that searches elements in a given range for the pattern that is specified in the constructor of searcher algorithm. Here is a simplified syntax:
1 2 3 |
std::search( first_data, last_data, search_method); |
C++17 includes a couple of specialized searcher methods, they are defined in <functional>
header. These are:
default_searcher
boyer_moore_searcher
boyer_moore_horspool_searcher
Here is an example how we can use std::search
in modern C++.
1 2 3 4 5 6 |
std::string str = "This is an example string by LearnCPlusPlus.org"; std::string str2 = "LearnCPlusPlus.org"; auto result = search( str.begin(), str.end(), boyer_moore_searcher( str2.begin(), str2.end()) ); |
Scanning Algorithms (Generalized Sum Algorithms)
Moreover, in C++17 Standard Library, there are generalized sum algorithms that we can use to scan data and we can output results. These are:
1 2 3 4 5 6 7 8 9 |
std::exclusive_scan(first_data, last_data, output_container, init[, bin_op]); std::inclusive_scan(first_data, last_data, output_container[, bin_op[, init]]); std::transform_exclusive_scan(first_data, last_data, output_container, init, bin_op, un_op); std::transform_inclusive_scan(first_data, last_data, output_container, bin_op, un_op[, init]); |
Reduce and Transform Reduce
In addition, the following two reduce algorithms have been added to the C++17 standard library:
1 2 3 4 5 |
std::reduce(first_data, last_data[, init[, bin_op]]); std::transform_reduce(first_data, last_data, init, bin_op, un_op); |
We should note that, std::reduce
looks similar to the existing std::accumulate
method in C++, but it does not guarantee any particular order of operations.
Parallel Algorithms
One of the biggest changes in the C++17 Standard Library is support for parallel execution algorithms. These are more than 60 algorithms, such as sort()
, all_of()
, ind()
, transform()
, etc. We can use these algorithms with modern containers and iterators. You may find a few examples of these algorithms below.
Please see this paper for more details: https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/p0024r2.html
C++ Builder is the easiest and fastest C and C++ compiler and IDE for building simple or professional applications on the Windows operating system. It is also easy for beginners to learn with its wide range of samples, tutorials, help files, and LSP support for code. RAD Studio’s C++ Builder version comes with the award-winning VCL framework for high-performance native Windows apps and the powerful FireMonkey (FMX) framework for UIs.
There is a free C++ Builder Community Edition for students, beginners, and startups; it can be downloaded from here. For professional developers, there are Professional, Architect, or Enterprise versions of C++ Builder and there is a trial version you can download from here.