All posts in "STL"

Implementing set_match in One Line of Code

Published July 17, 2020 - 0 Comments

In the previous post we’ve implemented set_match, an algorithm on sets inspired from the STL ones, that pairs up matching elements between two sorted collections. Being an algorithm on sets, the code we wrote for it looks like a typical implementation of an algorithm on set: template<typename Set1, typename Set2, typename OutputIterator, typename Comparator> OutputIterator set_match(Set1&& […]

How to Check If 2 Sorted Collections Have a Common Element

Published July 3, 2020 - 0 Comments

Ah, the algorithms on sets! Such beautiful algorithms, and so useful too. The algorithms on sets are basically the algorithms that take sorted collections and compare them in linear time. The STL offers five algorithms on sets: std::set_difference, std::set_intersection, std::set_union, std::set_symmetric_difference, and std::includes. If you’re a C++ developer, you absolutely, positively, unquestionably need to know […]

How to Modify a Key in a C++ Map or Set

Published May 1, 2020 - 0 Comments

Contrary to sequence containers like std::vector, you can’t just assign a new value to a key of a std::map in C++, like this: auto myMap = std::map<std::string, int>{ {“one”, 1}, {“two”, 2}, {“three”, 3} }; myMap.find(“two”)->first = “dos”; Doing so makes the compiler output a copious amount of errors: error: no match for ‘operator=’ (operand types […]

How to Extract Words among Spaces in a C++ String

Published April 17, 2020 - 0 Comments

We’ve already seen how to split a string into words with a delimiter, but there is another use case that is pretty close, and that doesn’t have the same implementation: extracting words that are among spaces in a string. For example, from the following string: “word1 word2 word3 ” We’d like to extract 3 sub-strings: […]

Implementing a Line Filter by Using C++ Ranges

Published April 3, 2020 - 0 Comments

In the last post we implemented a line filter by using standard C++14 features (with a little help of Boost), with the following interface: auto const filteredText = join(‘\n’, filter(contains(words), split(‘\n’, text))); We had to implement join, filter and split, which had the following interfaces: std::string join(char delimiter, std::vector<std::string> const& inputs); template<typename T, typename Predicate> std::vector<std::string> […]

How to Pass Class Member Functions to STL Algorithms

Published March 6, 2020 - 0 Comments

The C++ standard library makes it easy to use free functions with its STL algorithms. For example, with std::transform, we can write code like this: auto const inputs = std::vector<int>{1, 2, 3, 4, 5}; auto const results = std::vector<int>{}; std::transform(begin(inputs), end(inputs), back_inserter(results), myFunction); This has the effect of calling myFunction on each element of inputs and putting […]

How to Make for_each Stop When a Condition Is True

Published February 18, 2020 - 0 Comments

std::for_each applies a function to each of the elements within a range: std::for_each(begin(v), end(v), f); But it doesn’t allow to stop somewhere in the range, when a condition becomes true on an element. Let’s see how to achieve this by using STL algorithms, and with more modern C++ libraries such as ranges and pipes. Stopping std::for_each […]

How to Make for_each Stop After N Elements

Published February 14, 2020 - 0 Comments

for_each is an STL algorithm that takes a range (in the form of two iterators) and a function, and applies the function to each element of the range: std::for_each(begin(v), end(v), f); // applies f to each element of v It is arguably the simplest algorithm of the STL library. But it is so simple that sometimes […]