C++C++17C++20IteratorsLearn C++Syntax

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. 

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:

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

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:

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++.

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:

Reduce and Transform Reduce

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

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

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

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.

Oh hi there 👋
It’s nice to meet you.

Sign up to receive awesome C++ content in your inbox, every day.

We don’t spam! Read our privacy policy for more info.

About author

Dr. Yilmaz Yoru has 35+ years of coding with more than 30+ programming languages, mostly C++ on Windows, Android, Mac-OS, iOS, Linux, and some other operating systems. He graduated and received his MSc and PhD degrees from the Department of Mechanical Engineering of Eskisehir Osmangazi University. He is the founder and CEO of ESENJA LLC Company. His interests are Programming, Thermodynamics, Fluid Mechanics, Artificial Intelligence, 2D & 3D Designs, and high-end innovations.
Related posts
C++C++17C++20Learn C++

How To Use std::apply With Tuple In C++ 17 And Beyond?

C++C++17C++20Learn C++NumericsSyntax

How To Compute The Greatest Common Divisor And Least Common Multiple in C++?

C++C++17Language FeatureLearn C++

How To Use Skia Shader SkSL Shading Language Code in C++ Builder?

Artificial Intelligence TechC++C++17C++20Learn C++

How To Create Simple Generative AI Code In C++