How to Use the Library Algorithms in C++14

This is a blog post about how to use the new library algorithms from c++14. I will focus on the new features (and some of the old ones) that are not in C++11.

This is a series of posts on the C++ Library Algorithms. In this post, we look at how to use the new algorithms introduced in C++14. In the next post, we look at some of the new features that are not in C++11.

The introduction of std::find_if in C++98 was a big deal. It allowed you to do something that was previously impossible: find an element by applying a predicate to it, instead of comparing it to something else. This made it possible to write code like this:

But now there’s a problem: what if you want to find more than one element? If you want to find two elements with different values, then you have to go through the whole list twice – once for each element – and that’s not good for performance.

So what do we do? Well, we could use std::find_if again:

But now we have two problems: first, we have to go through the whole list twice; second, even if you have a predicate that

This blog post is about how to use the new library algorithms from C++14 in your code. The new library algorithms are based on the existing Standard Template Library (STL) algorithms and extend them with the following features:

Creating a range from an iterator pair or a container.

Iterating over a subrange defined by two iterators or two indexes.

Using a predicate function or function object to determine if an input sequence should be included in the output sequence.

Using a binary predicate function or function object for comparing values, e.g., for sorting.

Using lambda expressions as a convenient way to pass predicates as arguments to algorithms.

C++11 was a major step forward for the language. One of its most important new features was the introduction of STL algorithms and containers. C++11 introduced a lot of new functionality to the existing library, but a lot of functionality that people had come to expect in their libraries were still missing.

Now, finally, in c++14, we have all of these things, and more! This is part 1 of a series covering these new things. In this post I’ll cover the new library algorithms. In part 2 I’ll cover smart pointers made easier with c++14. And in part 3 I’ll cover variable templates and type deduction.

C++14 added a ton of new features, one of which was the arrival of new library algorithms. These are really useful because they abstract away a lot of code you’d need to write in order to do certain operations on containers. In this post, I’ll show you how to use some of them.

Now, before we get into it, I just want to mention that these algorithms can be used with any container type (so long as the container has the appropriate members defined). That includes arrays and strings. So there’s no reason why you shouldn’t be making use of them!


First up is for_each. This algorithm allows us to iterate through every element in a container and do something with each one. It takes an iterator range and a function object that will be called with each element in the range. In modern C++, we can use lambdas as function objects, so these are going to be our workhorse functions in this post.

I have been using the new algorithms in C++14 for a while, and I have to say that they are really great. For example, instead of writing something like this:


void erase_if(Container& c, Pred p) {

auto it = std::begin(c);

while (it != std::end(c)) {

if (p(*it)) {

it = c.erase(it);

} else {





with C++14 we can write the following:


void erase_if(Container& c, Pred p) {

auto it = std::remove_if(std::begin(c), std::end(c), p); // move the non-matching elements to the beginning of the container.

c.erase(it, std::end(c)); // remove the moved elements from the end of the container. This can be done because containers provide an overload of erase that takes two iterators as arguments. (This is not true of all

The C++ Standard Library provides a set of common classes and interfaces that greatly extend the core C++ language. The library, however, is not self-explanatory. To make full use of its components–and to benefit from their power–you need a resource that does far more than list the classes and their functions. You need a resource that explains when and why to use a particular feature, and that describes in detail how that feature works.

The Definitive C++ Book Guide and List is one of the most complete resources on the Internet for C/C++ books. We have collected all the books we could find in all major languages (English, Spanish, German and French) about C/C++ programming. All this data has been structured in an Excel spreadsheet so you can easily access it and filter it by year of publication or language. Also, we have added links to Amazon so you can order any book directly from our sheet.

C++ is a language that has been used in industry for decades. It’s widely regarded as one of the most powerful programming languages in existence. However, it has also acquired a reputation for being difficult to learn and use.

That’s why many people prefer to use other languages, such as Java or C

Leave a Reply

Your email address will not be published. Required fields are marked *