Tag Archives for " iterator "

How to Implement operator= When a Data Member Is a Lambda

Published October 2, 2020 - 0 Comments

In C++, some types of class members make it tricky to implement a copy assignment operator, operator=. For example references, const members, and… lambdas. Indeed, in the majority of cases, lambdas don’t have an operator=. (In case you’re wondering in what case lambdas have an operator=, it is in C++20 and when they don’t capture anything.) […]

How to Check If an Inserted Object Was Already in a Map (with Expressive Code)

Published September 27, 2019 - 0 Comments

To insert a new entry into an STL set or map, or any of their multi- and unordered- equivalents, we use the insert method: std::map<int, std::string> myMap = // myMap is initialized with stuff… myMap.insert({12, “twelve”}); insert performs the action of inserting the new entry into the container, if that entry wasn’t there already. But insert doesn’t only […]

Chaining Output Iterators Into a Pipeline

Published August 6, 2019 - 0 Comments

We’ve been over a various set of smart output iterators over the past few weeks. Today we explore how to combine them and create expressive code. If you’re just joining our series on smart output iterators, you might want to check out this introductory post on smart output iterators. So far, we’ve been combining smart […]

The Demultiplexer Iterator: Routing Data to Any Numbers of Outputs

Published March 12, 2019 - 0 Comments
demux demultiplexer iterator

In a previous post, we explored the partition output iterator, that routes data into two directions according to a predicate: the elements that satisfy the predicate to one side, and those that don’t to another side: auto const isEvenPartition = partition([](int n){ return n % 2 == 0; }); std::copy(begin(input), end(input), isEvenPartition(back_inserter(evenNumbers), back_inserter(oddNumbers))); The above code […]

The design of the STL

Published April 18, 2017 - 7 Comments

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 smart iterator for inserting into a sorted container in C++

Published March 17, 2017 - 5 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). […]