C++ offers more functionalities about sorting that meets the eye. Let’s see what the STL and Boost can do on this topic. Sorting a whole range The standard function to sort a whole range is std::sort. It operates in O(n*log(n)) and applies the sort directly on the passed range. The comparison used is operator< by default, […]
Today I want to share with you an interesting technique shown by Arthur O’Dwyer in his CppCon talk, Template Normal Programming, to deal with min and max on different types. Arthur has kindly accepted that I share this content with you on Fluent C++. This will be a shorter post, fit for summer as you […]
C++14 brought a interesting feature on associative containers that was cruelly missing for certain use cases: the ability to search an associative container with something that is semantically a key, even if it is not technically a key. This fills a very similar need as the one in Functors are not dead: the double functor trick, but […]
In C++11, a host of new features were introduced in the language and the Standard Library, and some of them work in synergy. Move iterators are an example of how the STL collaborates with move semantics, to allow expressing several important concepts in a very well integrated piece of code. Well, almost. By using the native features only, we don’t get […]
How to split a string in C++? That is to say, how to get a collection of substrings representing the words of a sentence, or the pieces of data contained in a CSV entry? This is a simple question, but one which has multiple answers in C++. We will see 3 solutions, each one having advantages and […]
As a logical part of the STL learning resource, here is how the STL has been designed, and how you can design your components to make them benefit from the power of the STL. The design of the STL has been driven by the intention of separating algorithms from data structures. Algorithms include: those in the […]
A couple of days ago, the range-v3 library got a new component: the view::cartesian_product adaptor. Understanding what this component does, and the thought process that went through its creation is easy and will let you have a better grasp of the range library. (Note that you could just as well understand all the following by looking at the […]
Min and max are such simple functions that there is not much to say about them… or are they? The basic algorithms min, max Functionally speaking, std::min and std::max are doing simple things indeed: they take two values, and return the smaller of the two for std::min and the bigger of the two for std::max. Here […]
A couple of weeks ago, I had the chance to hear some feedback from Stephan T. Lavavej about the STL learning resource on Fluent C++. This was done during an episode of CppCast (the podcast for C++ developers by C++ developers) where he was a guest. Stephan said that he found it overall pretty good (yay!) but he had […]
This post is part of the STL learning resource. Today we focus on how to fill out a range or a container with the STL. std::fill and std::uninitialized_fill std::fill takes a range and a value, and sets all elements of the range as equal to this value. vector<int> v = {1, 2, 3, 4, 5}; fill(v.begin(), v.end(), 3); […]