Using the STL map Class

Click here to change the theme.

The following is a sample use of the STL map Class. Using the set class is very similar and a little easier.

#pragma warning(disable:4786)
#include <map>
#include <string>
#include <iostream>

using namespace std ;

typedef map<string, string> StringMap;

void ShowFindStringMap(StringMap Set, string String) {
cout << String.c_str() << ((Set.find(String)==Set.end()) ? " was not found\n" : " was found\n");

void main() {
	StringMap::iterator sit;
	StringMap s2;
s2.insert(StringMap::value_type("One", "First"));
s2.insert(StringMap::value_type("Two", "Second"));
s2.insert(StringMap::value_type("Eight", "Last"));
cout << "Map contains:\n";
for (sit=s2.begin(); sit!=s2.end(); sit++)
	cout << '\t' << sit->first << " = " << sit->second << endl;
ShowFindStringMap(s2, "Two");
ShowFindStringMap(s2, "Three");

Hosted by