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 […]
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 […]
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> […]
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 […]
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 […]
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 […]
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 […]
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 […]
One thing that is cruelly lacking with std::inserter is that it can do just this: inserting. In some situations this is not enough, in particular for a map: what if an element with the same key is already there? std::inserter, since it calls std::map::insert, will not do anything at all in this case. But maybe we would […]
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). […]