Published a few days ago

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.

1 2 3 4 |
vector<int> v = {1, 2, 3, 4, 5}; fill(v.begin(), v.end(), 3); // v contains {3, 3, 3, 3, 3}; |

std::fill calls operator= on each element. std::uninitialized_fill does essentially the […]

Published 5 days ago
- 2 Comments

One thing that is cruelly lacking with std::inserter is that it can do just this: inserting. In some situations this is not enough, in particular for a map: what if an element with the same key is already there? std::inserter, since it calls std::map::insert, will not do anything at all in this case. But maybe we would […]

Published last week
- 2 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). […]

Published a couple of weeks ago
- 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 […]

Published last month
- 4 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 […]

Published last month
- 2 Comments

Let’s start with the following code excerpt:

1 2 3 4 5 6 7 8 9 10 11 |
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 by 2 sorted vectors v1 and v2, on which we apply a std::set_difference (see Algorithms on sets). This std::set_difference writes its output in results, with std::back_inserter ensuring that all outputs are push_back’d into results. One particularity though: a custom comparison operator […]

Published last month
- 1 Comment

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:

1 2 3 4 |
template<typename InputIterator, typename OutputIterator, typename UnaryOperation> OutputIterator transform(InputIterator first1, InputIterator last1, OutputIterator result, UnaryOperation op); |

As soon as you start working with the STL the need for std::transform appears. For example, to obtain the […]

Published last month
- 5 Comments

In the post describing algorithms on sets, we’ve been over what the STL offers to manipulate sets, that are sorted collection of elements – and not only std::sets. I’ve witnessed my code and the one of the people around me grow with these algorithms, for the better. Being rid of low-level for loops clears up the air […]

Published last month
- 1 Comment

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

Published last month

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