From: Jonathan W <jwi...@ea...> - 2001-03-22 18:28:22
|
Did I briefly go over maps and multimaps? A map contains elements in key/value pairs. In the following example, a first name is paired with an age. multimaps is like maps except that multiple keys are excepted. Here is an example with maps: #include <iostream> #include <map> // needed to use maps #include <string> using namespace std; int main() { map<string,int> ageMap; // insert values ageMap["Adam"]=23; ageMap["Bob"]=45; ageMap["Christy"]=26; // print values map<string,int>::iterator pos; //pos for position for(pos=ageMap.begin(); pos!=ageMap.end(); pos++) { cout << "Name: " << pos->first << "\t\tAge: " << pos->second << endl; } return 0; } It can also define a typedef (alias) as being type map<string,int>, to make sure that the iterator and the map is created the same. For example: typedef<string,int> StringIntMapType; then: StringIntMapType ageMap; and later: StringIntMapType::iterator pos; --Jonathan Williford _______________________________________________ cpptraining mailing list cpp...@ma... https://mail.worldforge.org/lists/listinfo/cpptraining |