-
Book Overview & Buying
-
Table Of Contents
C++ STL Cookbook - Second Edition
By :
The map class is an associative container that holds key-value pairs, where keys must be unique within the container.
There are a number of ways to populate a map container. Consider a map defined like this:
map<string, string> m;
We can add an element with the [] operator:
m["Miles"] = "Trumpet"
We can use the insert() member function:
m.insert({"Hendrix", "Guitar"});
Here we use template argument deduction to pass a std::pair of values to the m.insert() function.
Or, we can use the emplace() member function:
m.emplace("Krupa", "Drums");
I tend to gravitate toward the emplace() function. Introduced with C++11, emplace() uses perfect forwarding to create the new element in place by forwarding parameters directly to the container's constructor. This is quick, efficient, and easy to code.
Though it's certainly an improvement over...
Change the font size
Change margin width
Change background colour