All posts in "STL"

A smart iterator for inserting into a sorted container in C++

Published March 17, 2017 - 5 Comments

Smart iterators add great potential to writing expressive code with the STL in C++. And the ones that are proposed natively work particularly well with vectors and with other sequence containers such as deque, list and string. But the situation is not as good for associative containers, such as maps and sets (or their flat- non-standard counterparts). […]

Functors are not dead: the double functor trick

Published March 9, 2017 - 8 Comments

When C++11 arrived, lambdas were massively used in the places where functors used before. Lambdas are more elegant, involve less typing and can do pretty much all that functor did. Pretty much. But not quite. We covered how to make code expressive by using lambdas in a dedicated post, but there are a few use […]

Predicates on ranges with the STL

Published February 23, 2017 - 6 Comments

In this episode of the STL learning resource, we see algorithms that can be used in a variety of contexts but that have one thing in common: they return a boolean characteristic of one or several ranges. The *_of series The STL provides 3 algorithms that indicates whether all, some, or none of the elements of a range satisfy […]

Custom comparison, equality and equivalence with the STL

Published February 16, 2017 - 2 Comments

Let’s start with the following code excerpt:  std::vector< std::pair<int, std::string> > v1 = … // v1 is filled with data std::vector< std::pair<int, std::string> > v2 = … // v2 is filled with data std::vector< std::pair<int, std::string> > results; std::sort(v1.begin(), v1.end()); std::sort(v2.begin(), v2.end()); std::set_difference(v1.begin(), v1.end(), v2.begin(), v2.end(), std::back_inserter(result), compareFirst); There are 2 sets of data represented […]

std::transform, a central algorithm

Published February 13, 2017 - 3 Comments

std::transform is a very useful algorithm. Let’s see what it can do. This post is part of the STL learning resource. std::transform on a range Essentially, std::transform applies a function to each element of a range: Here is its prototype: template<typename InputIterator, typename OutputIterator, typename UnaryOperation> OutputIterator transform(InputIterator first1, InputIterator last1, OutputIterator result, UnaryOperation op); As soon as you […]

The searching <algorithm>s the STL holds secret

Published February 2, 2017 - 5 Comments

Let’s wrap up the series about searching with the STL by reviewing a handful of algorithms that are much less known than those presented in the other posts, but can prove themselves quite useful. Here is the series about searching with the STL: How to (std::)find something efficiently with the STL: covering classical STL algorithms for performing searches […]

Searching when you have access to an STL container

Published January 26, 2017 - 2 Comments

After seeing how to search for values in a range delimited by iterators, let’s see how to operate efficiently when you directly have access to a C++ container. This is the second one in the series about searching with the STL: How to (std::)find something efficiently with the STL: covering classical STL algorithms for performing searches on ranges of […]

STL Function objects: Stateless is Stressless

Published January 23, 2017 - 4 Comments

The need for function objects arises almost as soon as you start using the STL. This post shows how to design them so that they contribute in making your code using the STL more expressive and more robust.   Function objects Here is a brief recap on function objects before getting to the meat. If […]