Question

I'm trying to access value and key using *iterator as shown :

for (Map<K, V>::iterator it = map.begin(); it != map.end(); ++it) {
V& value = (*it).getValue(); // or*it.getValue();
}

it does not work,even the drop menu does not show anything (working with eclipse) . the only way to get it working is to do the following :

Node<K, V>& node = *it;
V& value = node.getValue();

I have both const and none const getters,also the* operator return reference to the node.

edit:
i'm implementing my own map code.

edit 2 I figured it out, my * operator was returning a pointer and i was thinking it returns & . thank you all

Was it helpful?

Solution

If the map type is std::map, or something similar that conforms to the standard Associative Container interface, then it contains key-value pairs, of type std::pair<const K, V>, and that is what you get when you dereference the iterator.

To access the value, you need the second element of the pair:

V & value = it->second;

If the map type doesn't conform to the standard interface, then you'll need to look at its documentation; I can't guess how it might work.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top