Yearly Archives: 2017

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 […]

What 70 people came up with on expressive code

Published February 6, 2017 - 7 Comments

Last week held the monthly meetup of Software Craftsmanship in my area. The organizer, Stéphane Bagnier, proposed a very original and interesting schedule for the evening. After a short series of brief and spontaneous presentations, any attendee could propose a topic he found worth spending time talking about, and then everyone got to vote. The 2 topics receiving the […]

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 […]

How to choose good names in your code

Published January 30, 2017 - 7 Comments

Naming is SO important. If your code is going to be read at least one time — if only by yourself — then names will play a major part in your capacity to work with it. Variable names, function names, class names, names in an interface, all are priceless ways to let your code tell more about […]

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 […]

Making code expressive with lambdas

Published January 19, 2017 - 22 Comments

Lambdas are arguably one of the most noted addition to the language in C++11. It is a useful tool, but one has to make sure to use them correctly to make code more expressive, and not more obscure. First off, let’s make clear that lambdas do not add functionalities to the language. Everything you can do […]

How to (std::)find something efficiently with the STL

Published January 16, 2017 - 5 Comments

This series of posts aims at covering all there is to know in the STL (and even sligthly beyond) about searching. Even though the need for searching for something in a collection is quite a simple concept to comprehend, there are many things to say to cover the topic thoroughly. Even if we’ll remain focused on how to practically […]