C++ map traversal


Quick example program with a C++ map, including how to iterate through the sample data. We should refer to the data in the map as keys and values (keys are unique indexes into their associated value). I have noticed that when I try to access a key that is not in the map, it returns an empty string.
#include <map>
#include <string>
#include <iostream>
 
using namespace std;
 
int main(int argc, char **argv)
{
    map<string, string> m;
    m["Harold Spencer"] = "515 Northland Dr NW";
    m["Jackson Porter"] = "7003 Parkway Avenue SE";
    m["Janice Heinz"] = "441 East 400 North";
 
    map<string, string>::iterator curr,end;
    for( curr = m.begin(), end = m.end(); curr != end; curr++ )
        cout <<  curr->first << " : " << curr->second << endl;
 
    map<string, string>::reverse_iterator i,ix;
    for( i = x.rbegin(), x = x.rend(); i != ix; i++ )
        cout <<  i->first << " : " << i->second << endl;
 
    return 0;
}

using boost:
//sudo apt-get install libboost-dev;
#include <iostream>
#include <string>
#include <boost/unordered_map.hpp>
 
typedef boost::unordered_map<std::string, int> umap;
 
int main(int argc, char **argv)
{
    umap m;
    m["one"] = 1;
    m["two"] = 2;
    m["three"] = 3;
 
    for(umap::iterator curr = m.begin(), end = m.end();  curr != end;  curr++ )
        std::cout <<  curr->first << " : " << curr->second << std::endl; 
 
   return 0;
}

using c++11:
//g++ main.cpp "-std=c++0x"
#include <iostream>
#include <string>
#include <unordered_map>
 
typedef std::unordered_map<std::string, int> umap;
 
int main(int argc, char **argv)
{
    umap m = { { "one", 1 }, { "two", 2 }, { "three", 3 } };
 
    for(umap::iterator curr = m.begin(), end = m.end();  curr != end;  curr++ )
        std::cout <<  curr->first << " : " << curr->second << std::endl; 
 
   return 0;
}
code snippets are licensed under Creative Commons CC-By-SA 3.0 (unless otherwise specified)