Reputation: 17486
In below codes,
std::map<Key,Int>::iterator p;
p = randomTable[chunk].find(value);
if (p != randomTable[chunk].end()) {
} else {
}
How does the p != randomTable[chunk].end()
works?
Does this mean, for all elements in randomTable that is not the last (end()
) element?
Upvotes: 1
Views: 117
Reputation: 27633
No. In the documentation for std::map<K, V>::find
, you can see:
Return Value
An iterator to the element, if the specified key value is found, or map::end if the specified key is not found in the container.
So, that if
statement is just checking that you actually found something.
Upvotes: 3
Reputation: 7691
find(value) will return the same as end() if value is not in the map. So if value is in the map, you'll go into the "if" branch, and if not, you'll go into the "else" branch.
end() isn't actually a member of the map - it points to the "element after the end". This is a slightly odd idea, but it probably helps to think that the search has looked at all of the elements in the map and failed to find the one you're looking for.
Upvotes: 5