C++ Associative ArrayQuick example program with a C++ map, including how to iterate through the sample data. We should refer to the data in the map as keys and values (keys are unique indexes into their associated value). I have noticed that when I try to access a key that is not in the map, it returns an empty string.
#include <map> #include <string> #include <iostream> using namespace std; int main(int argc, char **argv) { map<string, string> m; m["Harold Spencer"] = "515 Northland Dr NW"; m["Jackson Porter"] = "7003 Parkway Avenue SE"; m["Janice Heinz"] = "441 East 400 North"; map<string, string>::iterator curr,end; for( curr = m.begin(), end = m.end(); curr != end; curr++ ) cout << curr->first + " : " + curr->second << endl; return 0; } Tags: c++ | Related Articles |