Problem with map upper_bound

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Henrik Goldman

    #1

    Problem with map upper_bound

    Hello,

    Assume we have a std::map<time_t , int>.
    Now I would like to find all values (second) which fits in (within the time
    range of first) between first of April 1st. and end of July. Notice that we
    did not yet reach end of July yet.
    Using lower_bound I can easily find the first data value within this range.
    However with upper_bound I get an invalid iterator when trying to get a
    future data. Ideally I would like the last record which has the highest time
    value.

    Maybe I should use some combination with lower_bound instead?

    -- Henrik


  • Greg Herlihy

    #2
    Re: Problem with map upper_bound

    On 7/5/07 12:52 PM, in article 468d4c24$0$887$ edfadb0f@dread1 2.news.tele.dk,
    "Henrik Goldman" <henrik_goldman @mail.tele.dkwr ote:
    Assume we have a std::map<time_t , int>.
    Now I would like to find all values (second) which fits in (within the time
    range of first) between first of April 1st. and end of July. Notice that we
    did not yet reach end of July yet.
    Using lower_bound I can easily find the first data value within this range.
    However with upper_bound I get an invalid iterator when trying to get a
    future data. Ideally I would like the last record which has the highest time
    value.
    std::map's upper_bound() method returns an iterator to the first element
    whose key is greater than the specified search key (or the end() iterator if
    no such element exists in the map).

    So, provided that upper_bound() does not return an iterator equal to begin()
    (meaning that all elements in the map are greater than the search key), then
    simply decrementing the iterator that upper_bound() returns - will give you
    an iterator to the last element in the map whose key value is less than or
    equal to the specified search key.

    Greg

    Comment

    • =?ISO-8859-1?Q?Erik_Wikstr=F6m?=

      #3
      Re: Problem with map upper_bound

      On 2007-07-06 00:06, Greg Herlihy wrote:
      On 7/5/07 12:52 PM, in article 468d4c24$0$887$ edfadb0f@dread1 2.news.tele.dk,
      "Henrik Goldman" <henrik_goldman @mail.tele.dkwr ote:
      >
      >Assume we have a std::map<time_t , int>.
      >Now I would like to find all values (second) which fits in (within the time
      >range of first) between first of April 1st. and end of July. Notice that we
      >did not yet reach end of July yet.
      >Using lower_bound I can easily find the first data value within this range.
      >However with upper_bound I get an invalid iterator when trying to get a
      >future data. Ideally I would like the last record which has the highest time
      >value.
      >
      std::map's upper_bound() method returns an iterator to the first element
      whose key is greater than the specified search key (or the end() iterator if
      no such element exists in the map).
      >
      So, provided that upper_bound() does not return an iterator equal to begin()
      (meaning that all elements in the map are greater than the search key), then
      simply decrementing the iterator that upper_bound() returns - will give you
      an iterator to the last element in the map whose key value is less than or
      equal to the specified search key.
      I'd just like to point out (though it might be obvious) that if the
      latter date will always be a future date then you can always just use
      end() directly and not use the more time consuming upper_bound().

      --
      Erik Wikström

      Comment

      • James Kanze

        #4
        Re: Problem with map upper_bound

        On Jul 6, 12:06 am, Greg Herlihy <gre...@pacbell .netwrote:
        On 7/5/07 12:52 PM, in article 468d4c24$0$887$ edfad...@dread1 2.news.tele.dk,
        "Henrik Goldman" <henrik_gold... @mail.tele.dkwr ote:
        Assume we have a std::map<time_t , int>.
        Now I would like to find all values (second) which fits in (within the time
        range of first) between first of April 1st. and end of July. Notice that we
        did not yet reach end of July yet.
        Using lower_bound I can easily find the first data value within this range.
        However with upper_bound I get an invalid iterator when trying to get a
        future data. Ideally I would like the last record which has the highesttime
        value.
        std::map's upper_bound() method returns an iterator to the first element
        whose key is greater than the specified search key (or the end() iteratorif
        no such element exists in the map).
        So, provided that upper_bound() does not return an iterator equal to begin()
        (meaning that all elements in the map are greater than the search key), then
        simply decrementing the iterator that upper_bound() returns - will give you
        an iterator to the last element in the map whose key value is less than or
        equal to the specified search key.
        But of course, he probably doesn't want an iterator to the last
        element. If he wants to find all values in the range, he is
        probably better off using the half open interval returned by
        lower_bound and upper_bound. (The functions have been designed
        as they were for a reason.)

        --
        James Kanze (GABI Software) email:james.kan ze@gmail.com
        Conseils en informatique orientée objet/
        Beratung in objektorientier ter Datenverarbeitu ng
        9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

        Comment

        Working...