#include #include #include #include #include #include using namespace std; // map // see Generic Programming and the STL, Austern, Chapter 16 // or Stroustrup, Sec 17.4 struct ltstr { bool operator()(const char *s1, const char *s2) const { return strcmp(s1, s2) < 0; } }; int main (void) { map value; char *foo = "twenty-two"; // --| init table |-- value["eleven"] = "11"; value["twelve"] = "12"; value[foo] = "22"; value["thirty-three"] = "33"; printf ("value.size() = %d\n", value.size()); printf ("value[thirty-three] = %s\n", value["thirty-three"]); printf ("value[forty-three] = %s\n", value["forty-three"]); // inserts (null) printf ("\n"); map::iterator p = value.find("eleven"); printf ("key[eleven] = %s, %s\n", (*p).first, (*p).second); p++; printf ("next key = %s, value = %s\n", (*p).first, (*p).second); p++; printf ("next key = %s, value = %s\n", (*p).first, (*p).second); p++; printf ("next key = %s, value = %s\n", (*p).first, (*p).second); p++; printf ("next key = %s, value = %s\n", (*p).first, (*p).second); printf ("\n"); value.erase("forty-three"); printf ("Erase forty-three!\n"); if (value.find("forty-three") == value.end()) { printf ("key[forty-three] not found\n"); } else { printf ("key[forty-three] = %s\n", value["forty-three"]); } printf ("\n"); for (p = value.begin(); p != value.end(); p++) { printf ("key = %s, value = %s\n", (*p).first, (*p).second); } printf("\n"); printf("Now repeat using map with default comparator (ptr)\n"); map xvalue; // --| init table |-- xvalue["eleven"] = "11"; xvalue["twelve"] = "12"; xvalue["twenty-two"] = "22"; xvalue["thirty-three"] = "33"; map::iterator pp; for (pp = xvalue.begin() ; pp != xvalue.end(); pp++) { printf ("key = %s, xvalue = %s\n", (*pp).first, (*pp).second); } return 0; }