The HashMap example shown next illustrates how to create a hash map: - C++ STL

C++ examples for STL:unordered_map

Description

The HashMap example shown next illustrates how to create a hash map:

Demo Code

#include <iostream>
#include <unordered_map>
#include <string.h>

using namespace std;

struct eqstr/*from   w  w w. j  av  a  2 s .  c o  m*/
{
    bool operator()(const char* s1, const char* s2) const
    {
        return strcmp(s1, s2) == 0;
    }
};

int main()
{
    unordered_map<const char*, int, hash<const char*>, eqstr> Colors;

    Colors["Blue"] = 1;
    Colors["Green"] = 2;
    Colors["Teal"] = 3;
    Colors["Brick"] = 4;
    Colors["Purple"] = 5;
    Colors["Brown"] = 6;
    Colors["LightGray"] = 7;

    cout << "Brown = " << Colors["Brown"] << endl;
    cout << "Brick = " << Colors["Brick"] << endl;

    cout << "NotIn = " << Colors["NotIn"] << endl;
}

Result


Related Tutorials