Reputation: 2002
I am using the Map type from the Stanford libraries. And I can easily get values, by passing it .get(key)
But it occurred to me that it would be beneficial if I could retrieve it´s keys
as-well. Is there a way of doing this?
According to it´s doc at: http://www.stanford.edu/class/cs106b/materials/cppdoc/Map-class.html it does not have such functions. But is there another way of doing it?
Maybe using an iterator: Map <string, int>::iterator
?
Upvotes: 0
Views: 546
Reputation: 168876
Yes, there is. Try foreach
:
As a simplification when iterating over maps, the foreach macro iterates through the keys rather than the key/value pairs.
Upvotes: 2
Reputation: 2104
Your map.h includes a comment:
Additional Map operations
- Iteration using the range-based for statement, standard STL iterators, and the mapping function mapAll
so, I guess you could try iterating with iterator:
Map<string, int> :: iterator it;
for(it = m.begin(); it != m.end(); ++it){ // Here m is your Map variable.
string key = it->first;
//... do something
}
Upvotes: 1