Screenplay: Inserting Elements

Subscript Operator

#include <map>
#include <string>
#include <iostream>

using MyMap = std::map<int, std::string>;

int main()
{
    MyMap my_map = {
        { 2, "zwei" },
        { 51, "einundfuenfzig" },
        { 54, "vierundfuenfzig" },
        { 346, "dreihundertsechsundvierzig" },
        { 1001, "tausendeins" },
        { 1002, "tausendzwei" },
    };

    my_map[7] = "sieben";                // <--- create new entry
    my_map[2] = "two";                   // <--- overwrites existing entry

    std::cout << my_map[2] << std::endl; // <--- will print "two"

    return 0;
}

insert()

  • Takes a pair (key, value)

  • Returns a pair (iterator, bool)

  • Good old pre C++11 way of handling all that

    #include <map>
    #include <string>
    #include <iostream>
    
    using MyMap = std::map<int, std::string>;
    
    int main()
    {
        MyMap my_map = {
            { 2, "zwei" },
            { 51, "einundfuenfzig" },
            { 54, "vierundfuenfzig" },
            { 346, "dreihundertsechsundvierzig" },
            { 1001, "tausendeins" },
            { 1002, "tausendzwei" },
        };
    
        std::pair<MyMap::iterator, bool> retval_7 =                        // <--- clumsy
            my_map.insert(std::make_pair(7, "sieben"));
        if (retval_7.second) {                                             // <--- inserted? (yes) 
            std::cout << "7 is in" << std::endl;
            std::cout << "key: " << retval_7.first->first << std::endl;    // <--- retval's "first" points to element
            std::cout << "value: " << retval_7.first->second << std::endl; // <--- retval's "first" points to element
        }
    
        std::pair<MyMap::iterator, bool> retval_2 =                        // <--- clumsy
            my_map.insert(std::make_pair(7, "sieben"));
        if (! retval_2.second)                                             // <--- inserted? (no)
            std::cout << "2 is not in" << std::endl;
    
        return 0;
    }
    
  • Same, using C++11 auto

    #include <map>
    #include <string>
    #include <iostream>
    
    using MyMap = std::map<int, std::string>;
    
    int main()
    {
        MyMap my_map = {
            { 2, "zwei" },
            { 51, "einundfuenfzig" },
            { 54, "vierundfuenfzig" },
            { 346, "dreihundertsechsundvierzig" },
            { 1001, "tausendeins" },
            { 1002, "tausendzwei" },
        };
    
        auto retval = my_map.insert(std::make_pair(7, "sieben"));       // <--- not so clumsy 
        if (retval.second) {
            std::cout << "7 is in" << std::endl;
            std::cout << "key: " << retval.first->first << std::endl;
            std::cout << "value: " << retval.first->second << std::endl;
        }
    
        return 0;
    }
    
  • Same, using C++17 Structured Binding

    #include <map>
    #include <string>
    #include <iostream>
    
    using MyMap = std::map<int, std::string>;
    
    int main()
    {
        MyMap my_map = {
            { 2, "zwei" },
            { 51, "einundfuenfzig" },
            { 54, "vierundfuenfzig" },
            { 346, "dreihundertsechsundvierzig" },
            { 1001, "tausendeins" },
            { 1002, "tausendzwei" },
        };
    
        auto [iter, inserted] = my_map.insert(std::make_pair(7, "sieben"));  // <--- even less clumsy 
        if (inserted) {                                                      // <--- cool
            std::cout << "7 is in" << std::endl;
            std::cout << "key: " << iter->first << std::endl;                // <--- cool
            std::cout << "value: " << iter->second << std::endl;             // <--- cool
        }
    
        return 0;
    }