RosettaCodeData/Task/Associative-array-Iteration/C++/associative-array-iteration-1.cpp

13 lines
428 B
C++
Raw Normal View History

2013-04-10 16:57:12 -07:00
std::map<std::string, int> myDict;
myDict["hello"] = 1;
myDict["world"] = 2;
myDict["!"] = 3;
// iterating over key-value pairs:
for (std::map<std::string, int>::iterator it = myDict.begin(); it != myDict.end(); it++) {
// the thing pointed to by the iterator is a pair<std::string, int>
std::string key = it->first;
int value = it->second;
std::cout << "key = " << key << ", value = " << value << std::endl;
}