Tag Archives for " map "

A Map with Two Types of Keys in C++

Published November 27, 2020 - 0 Comments

The need to associate keys to values is pretty common in computer programming. (That is a very general sentence, isn’t it?) In C++, the standard tools to achieve that are std::map and std::multimap that use comparisons on keys and std::unordered_map and std::unordered_multimap that use hashing. Boost adds flat_map, that offers a different performance trade-off and bimap to look […]

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

Inserting Values to a Map with Boost.Assign

Published December 3, 2019 - 0 Comments

Boost.Assign is a library that allows for a natural syntax to add elements to a container: std::vector<int> v; v += 1,2,3,4,5,6,7,8,9,10; We’ve seen how it works with vectors and sets, now we will focus on maps. Indeed, maps don’t work the same way. The following code, despite that it looks nice, doesn’t compile: #include <boost/assign/std/map.hpp> […]

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

Overview of std::map’s Insertion / Emplacement Methods in C++17

Published December 11, 2018 - 2 Comments
insert_or_assign try_emplace map C++

Today’s guest post is written by @walletfox, one of the hitchhikers in the C++ galaxy, trying to navigate its dark corners by writing articles, creating Mostly Harmless cheat sheets and observing the following rules: “Don’t panic! Always carry a cheat sheet next to the towel. So long and thanks for all the fish.”. Interested in writing on Fluent C++ too? Submit your […]

How to Remove Elements from an Associative Container in C++

Published September 21, 2018 - 11 Comments
remove mltimap map set multiset

Welcome back for our second part in our series on removing elements from C++ containers! How to Remove Elements from a Sequence Container (vector, string, deque, list) How to Remove Pointers from a Vector in C++ (co-written with Gaurav Sehgal) How to Remove Elements from an Associative Container (maps and sets) How to Remove Duplicates from […]

105 STL Algorithms in Less Than an Hour

Published July 10, 2018 - 0 Comments
105 STL algorithms in less than an hour

Everyone knows that it’s a good thing to know the STL algorithms. But do know each and every one of them? To learn all there is in the STL algorithms library, I’ve presented a talk at several conferences this year, that was titled 105 STL Algorithms in Less Than an Hour. The point of this […]

The World Map of C++ STL Algorithms

Published July 6, 2018 - 5 Comments
world map C++ STL algorithms

We all know that we should know our STL algorithms, because they help make our code more expressive and more robust (sometimes in spectacular ways!). But do you know all your STL algorithms? There are 105 of them if we include those of C++17, and every one of them has a chance to be useful […]

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