Access Pecularities with Pairs in std::Map
Feb 22, 2009 at 7:11pm UTC
Hey everyone,
This has always confused me. I'm using the default STL implementation provided by the MinGW suite, and when I use an iterator to the map, I can access the key and value if I don't dereference the iterator, like so:
1 2 3 4 5 6 7 8 9
using namespace std;
map<string,int > myMap;
...
map<string,int >::iterator it;
for (it = myMap.begin();it !=myMap.end();++it)
{
cout << "key is: " << it->first << endl;
cout << "value is: " << it->second << endl;
}
However, if I deref the iterator beforehand, as seen in this site's STL reference quide ([url]
http://cplusplus.com/reference/stl/map/begin.html[/url]), I get a compile-time error stating:
error: 'struct std::_Rb_tree_iterator<std::pair<const std::string, int> >' has no member named 'second'
It's also worth noting that my STL doesn't include the Pair class - I can't
#include <pair>
. Yet Map and non deref'd iterators work fine. Any ideas?
Feb 22, 2009 at 7:13pm UTC
#include <utility> for std::pair
operator* and operator-> on map iterators do two different things.
Topic archived. No new replies allowed.