Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > what does find in the MAP do if no match is found for the key

Reply
Thread Tools

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

 
 
NewToCPP
Guest
Posts: n/a
 
      12-14-2006
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?

 
Reply With Quote
 
 
 
 
NewToCPP
Guest
Posts: n/a
 
      12-14-2006
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???



NewToCPP wrote:
> 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?


 
Reply With Quote
 
 
 
 
kwikius
Guest
Posts: n/a
 
      12-14-2006

NewToCPP wrote:
> 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

 
Reply With Quote
 
kwikius
Guest
Posts: n/a
 
      12-14-2006

NewToCPP wrote:
> 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

 
Reply With Quote
 
Howard
Guest
Posts: n/a
 
      12-14-2006

"NewToCPP" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) ups.com...

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

>
> NewToCPP wrote:
>> 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?

>
> 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




 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
std::map::find() throws exception when map is empty? Matthias Hildebrand C++ 5 03-20-2012 06:09 AM
Map.get(key) returns null after inserting key Mize-ze Java 4 01-01-2007 04:31 PM
Partial key match in a map Ninan C++ 9 10-28-2005 09:41 PM
map::insert gets a hint. What about map::find? rudymoore@hotmail.com C++ 3 04-20-2005 01:11 AM
map.insert(key,val) vs. map[key]=val ? Patrick Guio C++ 6 10-20-2004 01:54 PM



Advertisments