The STL algorithms on sets are one of the most convenient things the C++ standard library offers. We’re going to see how they can all be implemented with the same core algorithm. This article is part of our series on algorithms on sets, which now includes: How to check if 2 sorted collections have a […]
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&& […]
The STL offers a handful of algorithms on sets. They are important to master, but they’re only the tip of the iceberg of what we can do with sets. In this post and the next few ones, we’re going to get deeper into the topic of algorithms on sets, by extending the algorithms on sets […]
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 […]
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 […]
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: […]
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> […]
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 […]
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 […]
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 […]