Count elements in range between two iterators

Discussion in 'C++' started by Henrik Goldman, Apr 23, 2006.

  1. I have a map structure containing the following information:

    ->first contains a time_t and is used for lookups
    ->second holds my associated data to this time

    When calculating averages over time I use lower_bound and upper_bound to
    find the actual data within the map.
    However in some cases no data exist in the range. What happens in this case
    is a valid result from lower_bound and upper_bound functions but
    iterator_begin will always be the same as iterator_end.
    Is there a library function which can give me the count of elements between
    two iterators? This way I can actually see if there are any results within
    the time range and thus won't get wrong results.

    Thanks in advance.
    -- Henrik
     
    Henrik Goldman, Apr 23, 2006
    #1
    1. Advertising

  2. Henrik Goldman wrote:
    > I have a map structure containing the following information:
    >
    > ->first contains a time_t and is used for lookups
    > ->second holds my associated data to this time
    >
    > When calculating averages over time I use lower_bound and upper_bound to
    > find the actual data within the map.
    > However in some cases no data exist in the range. What happens in this case
    > is a valid result from lower_bound and upper_bound functions but
    > iterator_begin will always be the same as iterator_end.
    > Is there a library function which can give me the count of elements between
    > two iterators? This way I can actually see if there are any results within
    > the time range and thus won't get wrong results.
    >
    > Thanks in advance.
    > -- Henrik
    >
    >

    You probably want std::distance().

    --
    VH
     
    Vaclav Haisman, Apr 23, 2006
    #2
    1. Advertising

  3. Henrik Goldman

    Guest

    Henrik Goldman wrote:
    > I have a map structure containing the following information:
    >
    > ->first contains a time_t and is used for lookups
    > ->second holds my associated data to this time
    >
    > When calculating averages over time I use lower_bound and upper_bound to
    > find the actual data within the map.
    > However in some cases no data exist in the range. What happens in this case
    > is a valid result from lower_bound and upper_bound functions but
    > iterator_begin will always be the same as iterator_end.
    > Is there a library function which can give me the count of elements between
    > two iterators? This way I can actually see if there are any results within
    > the time range and thus won't get wrong results.
    >
    > Thanks in advance.
    > -- Henrik

    To calculate the average, you surely must be counting the number of
    elements correct? Because you need to divide by that number to arrive
    at an average.

    Do the same thing you are doing, except before dividing, check if the
    number you counted is zero. If so, return something appropriate for
    this case (e.g 0?).
     
    , Apr 23, 2006
    #3
  4. Thanks to both of you for the quick answer.

    I settled in a simple compare between (itBegin == itEnd) under the
    assumption that if this case happens then I do some extra processing to see
    what goes on. In some cases I return 0 and other cases another value.

    Thanks again.

    -- Henrik
     
    Henrik Goldman, Apr 23, 2006
    #4
    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. Marcin Kaliciñski

    Iterators and reverse iterators

    Marcin Kaliciñski, May 8, 2005, in forum: C++
    Replies:
    1
    Views:
    493
    Kai-Uwe Bux
    May 8, 2005
  2. John
    Replies:
    19
    Views:
    1,125
    Richard Smith
    Mar 13, 2006
  3. , India
    Replies:
    10
    Views:
    1,085
    James Kanze
    Aug 8, 2009
  4. Steven D'Aprano

    Are range iterators thread safe?

    Steven D'Aprano, Oct 20, 2011, in forum: Python
    Replies:
    3
    Views:
    173
    Ian Kelly
    Oct 20, 2011
  5. George
    Replies:
    8
    Views:
    654
    Arne Vajhøj
    May 6, 2012
Loading...

Share This Page