Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   Count elements in range between two iterators (http://www.velocityreviews.com/forums/t453550-count-elements-in-range-between-two-iterators.html)

Henrik Goldman 04-23-2006 05:24 PM

Count elements in range between two iterators
 
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



Vaclav Haisman 04-23-2006 05:35 PM

Re: Count elements in range between two iterators
 
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

AnonMail2005@gmail.com 04-23-2006 05:48 PM

Re: Count elements in range between two iterators
 
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?).


Henrik Goldman 04-23-2006 06:32 PM

Re: Count elements in range between two iterators
 
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




All times are GMT. The time now is 08:12 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.