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

Discussion in 'C++' started by NewToCPP, Dec 14, 2006.

  1. NewToCPP

    NewToCPP Guest

    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?
    NewToCPP, Dec 14, 2006
    #1
    1. Advertising

  2. NewToCPP

    NewToCPP Guest

    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?
    NewToCPP, Dec 14, 2006
    #2
    1. Advertising

  3. NewToCPP

    kwikius Guest

    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
    kwikius, Dec 14, 2006
    #3
  4. NewToCPP

    kwikius Guest

    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
    kwikius, Dec 14, 2006
    #4
  5. NewToCPP

    Howard Guest

    "NewToCPP" <> wrote in message
    news:...

    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
    Howard, Dec 14, 2006
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Patrick Guio
    Replies:
    6
    Views:
    3,174
    chris
    Oct 20, 2004
  2. Mize-ze
    Replies:
    4
    Views:
    497
    Robert Klemme
    Jan 1, 2007
  3. Ninan
    Replies:
    9
    Views:
    1,419
  4. M P
    Replies:
    1
    Views:
    451
  5. Wybo Dekker
    Replies:
    1
    Views:
    337
    Yukihiro Matsumoto
    Nov 15, 2005
Loading...

Share This Page