what does find in the MAP do if no match is found for the key

N

NewToCPP

I checked the microsoft MSDN and it says the following:

"find returns an iterator that addresses the location of an element
with a specified key, or the location succeeding the last element in
the map if no match is found for the key."


What does it mean by the location succeeding the last element.... how
do we know that the element not found in the MAP?
 
N

NewToCPP

OOPS... incomplete question..


If no match is found for the key, end( ) is returned

I can check if the returned item == m1.end( ) ). If so, item not
found.. otherwise use the returned value...

Is there any other way to find out that the item with the given key is
not found???
 
K

kwikius

NewToCPP said:
I checked the microsoft MSDN and it says the following:

"find returns an iterator that addresses the location of an element
with a specified key, or the location succeeding the last element in
the map if no match is found for the key."


What does it mean by the location succeeding the last element.... how
do we know that the element not found in the MAP?

IIRC it returns a special iterator representing the end of the map(or
'one past the end' if you prefer)

Just compare the iterator returned:

if (my_map.find(id) == my_map.end()){
std::cout << id <<" not found\n";
}

regards
Andy Little
 
K

kwikius

NewToCPP said:
OOPS... incomplete question..


If no match is found for the key, end( ) is returned

I can check if the returned item == m1.end( ) ). If so, item not
found.. otherwise use the returned value...

Is there any other way to find out that the item with the given key is
not found???

There are other ways, but they all end up at around about the same
place AFAICS... IOW the end(). std::map is meant to be a black box, but
it is usually implemented as a tree. The map sorts all the ids so that
it can find them fast, hence using find() is likely to be the fastest
and best method of finding things in the map..or not as the case may
be. The iterators are usually only useful to enumerate through the map
to see whats in there

hmm .. I love std::map :)

regards
Andy Little
 
H

Howard

Please don't top-post. I've re-arranged your messages:
OOPS... incomplete question..


If no match is found for the key, end( ) is returned

I can check if the returned item == m1.end( ) ). If so, item not
found.. otherwise use the returned value...

Is there any other way to find out that the item with the given key is
not found???

What's wrong with comparing with end()? That's the standard way to tell if
the find failed. Is that not working for you?

-Howard
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,734
Messages
2,569,441
Members
44,832
Latest member
GlennSmall

Latest Threads

Top