Mapping strings to Other Things, Creating a string map - C++ STL

C++ examples for STL:map

Description

Mapping strings to Other Things, Creating a string map

Demo Code

#include <iostream>
#include <map>
#include <string>

using namespace std;

int main() {//from  ww  w  .  j  ava 2s  .c  o m

   map<string, string> strMap;

   strMap["Monday"]    = "M";
   strMap["Tuesday"]   = "D";
   strMap["Wednesday"] = "W";
   strMap["Thursday"]  = "T";
   strMap["Friday"]    = "F";

   strMap["Saturday"]  = "S";
   strMap.insert(pair<string, string>("Sunday", "Sun"));

   for (map<string, string>::iterator p = strMap.begin(); p != strMap.end(); ++p ) {
         cout << "English: " << p->first << ", German: " << p->second << endl;
   }

   cout << endl;

   strMap.erase(strMap.find("Tuesday"));
   for (map<string, string>::iterator p = strMap.begin(); p != strMap.end(); ++p ) {
         cout << "English: " << p->first << ", letter: " << p->second << endl;
   }
}

Related Tutorials