------------------------------------------------------------------------------
MC logo
STL Map 1
[^] CSc 220 Home
------------------------------------------------------------------------------
[Introduction] [Boolean and Control] [Functions] [Arrays and Pointers] [Dynamic Arrays] [Array Errors] [Command Array] [Standard Containers] [Structs and Classes] [Automatic Pointer] [Multi-File Programs] [Copying Objects] [Templates] [Inheritance] [Plain C I/O] [Type Odds And Ends] [Plain C Strings] [File I/O]
[STL Vector 101] [STL Sorting Vectors] [stlist_cpp] [STL Map 1] [STL Map 2] [Word Counter]
stlmap0.cpp
#include <iostream>
#include <string>
#include <map>
#include <stdlib.h>

using namespace std;

main()
{
        map<string, string> dict;

        dict["mouse"] = "squeek";
        dict["cat"] = "meow";
        dict["dog"] = "woof";
        dict["bird"] = "tweet";
        dict["snake"] = "sssss";

        map<string, string>::iterator i = dict.begin();
        for(; i != dict.end(); ++i)
                cout << i->first << ": " << i->second << endl;
}

The map takes a key type (any type that supports inquality comparison) and a data type (pretty much anything) and operates like an array subscripted by the key type. (This sort of structure is often known as a dictionary.)

It supports the common iterator construct, except each item has two parts, the key and data, known by the creative names first and second.