Site icon Learn C++

What Are The New Algorithms That Come With C++ 17?

What Are The New Algorithms That Come With C++ 17

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.

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. 

[crayon-6763ea23c4bac670662815/]
[crayon-6763ea23c4bb8390740975/]

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:

[crayon-6763ea23c4bbc560595602/]

Here is an example that generates sample from vec to vec2.

[crayon-6763ea23c4bbe183166547/]

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:

[crayon-6763ea23c4bc1245608704/]

C++17 includes a couple of specialized searcher methods, they are defined in <functional> header. These are:

Here is an example how we can use std::search in modern C++.

[crayon-6763ea23c4bc3541911655/]

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:

[crayon-6763ea23c4bc6808736173/]

Reduce and Transform Reduce

In addition, the following two reduce algorithms have been added to the C++17 standard library:

[crayon-6763ea23c4bc8714956131/]

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.

Exit mobile version