Tag Archives: map

Looping over the keys and values in a map

Say we have this map:

#include <map>
#include <string>

typedef std::map<std::string, unsigned int> map_string_to_uint;
const size_t n = 12;

map_string_to_uint monthdays;
const char *months[] 
        = {"JAN", "FEB", "MAR", "APR", "MAY", "JUN", "JUL", "AUG", "SEP", "OCT", "NOV", "DEC"};
unsigned int days[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
for (int m = 0; m < n; ++m) {
    monthdays[months[m]] = days[m];
}

And we want to loop over the keys and values.

Method 1: Use an iterator

    for (map_string_to_uint::const_iterator it = monthdays.begin(); it != monthdays.end(); ++it) {
        std::cout << it->first << ": " << it->second << "\n";
    }

Method 2: Use a range-based for loop (C++11)

    for (const auto& pair : monthdays) {
        std::cout << pair.first << ": " << pair.second << "\n";
    }

Method 3: Use Boost.Foreach

#include <boost/foreach.hpp>

BOOST_FOREACH(const map_string_to_uint::value_type& pair, monthdays) {
    std::cout << pair.first << ": " << pair.second << "\n";
}

Reference: Boost.Foreach

Populate a map with literal values

Method 1: Use parallel arrays

#include <map>
#include <string>

int main()
{
    typedef std::map<std::string, unsigned int> map_string_to_uint;
    const size_t n = 12;

    map_string_to_uint monthdays1;
    const char *months1[] = {"JAN", "FEB", "MAR", "APR", "MAY", "JUN",
        "JUL", "AUG", "SEP", "OCT", "NOV", "DEC"};
    unsigned int days1[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    for (int m = 0; m < n; ++m) {
        monthdays1[months1[m]] = days1[m];
    }
}

Method 2: Use an array of structs

#include <map>
#include <string>

int main()
{
    typedef std::map<std::string, unsigned int> map_string_to_uint;
    const size_t n = 12;

    map_string_to_uint monthdays2;
    struct Monthdays {
        const char *month;
        unsigned int days;
    };
    Monthdays months2[] =  {{"JAN", 31}, {"FEB", 28}, {"MAR", 31},
        {"APR", 30}, {"MAY", 31}, {"JUN", 30}, {"JUL", 31}, {"AUG", 31}, {"SEP", 30},
        {"OCT", 31}, {"NOV", 30}, {"DEC", 31}};
    for (int m = 0; m < n; ++m) {
        monthdays2[months2[m].month] = months2[m].days;
    }
}

Method 3: Use an array of std::pairs and the range constructor

#include <map>
#include <string>

int main()
{
    typedef std::map<std::string, unsigned int> map_string_to_uint;
    const size_t n = 12;

    std::pair<std::string, unsigned int> months3[] = {
        std::make_pair("JAN", 31), std::make_pair("FEB", 28), std::make_pair("MAR", 31),
        std::make_pair("APR", 30), std::make_pair("MAY", 31), std::make_pair("JUN", 30),
        std::make_pair("JUL", 31), std::make_pair("AUG", 31), std::make_pair("SEP", 30), 
        std::make_pair("OCT", 31), std::make_pair("NOV", 30), std::make_pair("DEC", 31)};
    map_string_to_uint monthdays3(months3, months3 + n);
}

Method 4: Use Boost.Assign

#include <map>
#include <string>
#include <boost/assign.hpp>

int main()
{
    typedef std::map<std::string, unsigned int> map_string_to_uint;

    map_string_to_uint monthdays4 = boost::assign::map_list_of ("JAN", 31) ("FEB", 28) ("MAR", 31)
        ("APR", 30) ("MAY", 31) ("JUN", 30) ("JUL", 31) ("AUG", 31) ("SEP", 30) 
        ("OCT", 31) ("NOV", 30) ("DEC", 31);
}

Method 5: Use a C++11 initializer

#include <map>
#include <string>

int main()
{
    typedef std::map<std::string, unsigned int> map_string_to_uint;

    /* Method 5: Use a C++11 initializer */
    map_string_to_uint monthdays5 =  {{"JAN", 31}, {"FEB", 28}, {"MAR", 31},
        {"APR", 30}, {"MAY", 31}, {"JUN", 30}, {"JUL", 31}, {"AUG", 31}, {"SEP", 30},
        {"OCT", 31}, {"NOV", 30}, {"DEC", 31}};
}