Thread: finding a position in stl map

  1. #1
    Registered User
    Join Date
    Jul 2006
    Posts
    25

    finding a position in stl map

    hi,

    i am looking for a function in STL that does this job:

    say, i have a map with elements a,b,d,e, and their values are 10,20, 30,40 respectively.

    i want a method, that gives me 20(or the position of "b") when i enter "c". i mean it finds the biggest element that has a key smaller than or equal to "c".

    another example
    a,b,c,d, and their values are 10,20, 30,40

    i want position of c, or 30; when i enter c

    another one:

    a,b,c,d, and their values are 10,20, 30,40

    i want position of d, or 40; when i enter e

    i think lower_bound, upper_bound may work out but i couldnt get them work. can you help me please?

    thanks
    Last edited by kolistivra; 08-11-2007 at 04:12 AM.

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    If the values are in the same sorted order as the keys, you could find the key and get to the previous key value with the iterator's decrement operator (first element is an exception).

    If they are not in the same order, lower and upper bound might not help (reading their description they seem to be meant for finding the beginning and end of a range of equal values). You might write a function that iterates the map and keeps track of the maximum value below some value. (I'm not too good with algorithm, may-be some of them might work for you - e.g max_element with a custom predicate. But then may-be a map isn't a good choice...)
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Yes, I believe lower_bound() is the right function.
    If you post the code you've tried, it would help us to figure out where the problem is.

    Chapter 85. Use the right STL search algorithm:
    http://www.ubookcase.com/book/Addiso...5lev1sec2.html

    Chapter 86. Use the right STL sort algorithm:
    http://www.ubookcase.com/book/Addiso...6lev1sec2.html

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    While that is my favorite C++ book and I would recommend that any C++ programmer buy it, I wonder if that site is legal.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Polynomials and ADT's
    By Emeighty in forum C++ Programming
    Replies: 20
    Last Post: 08-19-2008, 08:32 AM
  2. Button handler
    By Nephiroth in forum Windows Programming
    Replies: 8
    Last Post: 03-12-2006, 06:23 AM
  3. STL Map Object
    By nomes in forum C++ Programming
    Replies: 6
    Last Post: 09-11-2003, 01:51 PM
  4. Unsorted Map and Multimap :: STL
    By kuphryn in forum C++ Programming
    Replies: 5
    Last Post: 12-21-2002, 11:22 PM
  5. Searching STL Map Inside STL Map Object :: C++
    By kuphryn in forum C++ Programming
    Replies: 2
    Last Post: 11-14-2002, 09:11 AM