stl

What std::exchange does, and how to remember it--Jonathan Boccara

If you had troubles.

What std::exchange does, and how to remember it

by Jonathan Boccara

From the article:

std::exchange was introduced in the C++ standard library in C++14 under the header <utility>.

Its name suggests that it’s a general-purpose and useful function, and its template prototype working with any type confirms this impression.

I don’t know about you, but I always had a problem with std::exchange: I couldn’t remember what it was doing. I learnt several times the operations that std::exchange performs, but each time I forgot them soon after.

Then at some point it clicked: std::exchange is a setter returning the old value. Ignoring the name “exchange” but thinking of it as a “setter” helped me make it stick to my mind.

It might just be me having a hard time with std::exchange for some reason. But just in case you also have issues remembering the operations of std::exchange, let’s see why std::exchange has this meaning, and why this meaning is not obvious from its name.

This should help you remember it once and for all...

How to Merge Consecutive Elements in a C++ Collection--Jonathan Boccara

Simple and sweet.

How to Merge Consecutive Elements in a C++ Collection

by Jonathan Boccara

From the article:

Merging identical consecutive elements in a collection is a recurring need, in C++ or elsewhere in programming.

For example, we could want to aggregate a collection of hourly results into a collection of daily results: all the results of each day get aggregated into one for that day. In this case, being “identical” means being on the same day, and “aggregating” means taking two results with a common date, and creating a result at this date and with the sum of their amounts...

The Regular Expression Library--Rainer Grimm

A quick introduction.

The Regular Expression Library

by Rainer Grimm

From the article:

My original plan was it to write about the rules of the C++ Core Guidelines to the regex and chrono library, but besides the subsection title, there is no content available. I already wrote a few posts about time functionality. So I'm done. Today, I fill the gap and write about the regex library.

The Difference Between std::copy_backward and std::copy with Reverse Iterators--Jonathan Boccara

What do you think?

The Difference Between std::copy_backward and std::copy with Reverse Iterators

by Jonathan Boccara

From the article:

A couple of months ago, I made a talk at the ACCU conference about learning every algorithm there is in the STL. Amongst them, we covered std::copy_backward, that makes a copy of a source range to a destination range, starting from its end and working its way back to the beginning.

In the questions session at the end of the talk, attendant Oscar Forner rose an interesting point: is there any difference between performing a std::copy_backward versus performing a simple std::copy on the reverse iterators from the source collection?

Heterogeneous Lookup in Ordered Containers, C++14 Feature--Bartlomiej Filipek

Did you know?

Heterogeneous Lookup in Ordered Containers, C++14 Feature

by Bartlomiej Filipek

From the article: 

If you have a map of strings, like std::map<std::string, int> m; and you want to find some element by m.find("abc"). Do you have to pay the price and construct a std::string object? Can you optimize it?

Let’s have a look at one feature enabled in C++14 that might help optimize such container access...