------------------------------------------------------------------------------
MC logo
STL Map 2
[^] 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]
stmap.cpp
#include <iostream>
#include <string>
#include <map>
#include <stdlib.h>

using namespace std;

main()
{
        map<string, int> stoi;

        stoi["help"] = 1;
        stoi["ring"] = 2;
        stoi["sink"] = 3;
        stoi["bye"] = 4;

        while(1) {
                // Red the command in.
                string in;
                cout << "> ";
                cin >> in;
                if(!cin) break;

                // Find the command.
                map<string, int>::iterator i = stoi.find(in);
                if(i == stoi.end()) {
                        cout << "Unknown command " << in << endl;
                        continue;
                }

                // Process known commands.
                switch(i->second)
                {
                case 1:
                        cout << "Commands are help, ring, sink and bye."
                             << endl;
                        break;
                case 2:
                        cout << "Brrrriiinnnnng" << endl;
                        break;
                case 3:
                        cout << "Blub, blub, blub" << endl;
                        break;
                case 4:
                        exit(0);
                }
        }
}