Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Count elements in range between two iterators

Reply
Thread Tools

Count elements in range between two iterators

 
 
Henrik Goldman
Guest
Posts: n/a
 
      04-23-2006
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


 
Reply With Quote
 
 
 
 
Vaclav Haisman
Guest
Posts: n/a
 
      04-23-2006
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
 
Reply With Quote
 
 
 
 
AnonMail2005@gmail.com
Guest
Posts: n/a
 
      04-23-2006
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?).

 
Reply With Quote
 
Henrik Goldman
Guest
Posts: n/a
 
      04-23-2006
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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Getting the Week count for the Two date range George Java 8 05-05-2012 11:58 PM
plain iterators and reverse iterators on vector subramanian100in@yahoo.com, India C++ 10 08-08-2009 08:28 AM
removing elements invalidates only those iterators that had specifically pointed at the removed elements Alien C++ 6 09-21-2006 03:13 PM
distance between two iterators in map John C++ 19 03-13-2006 03:37 PM
Iterators and reverse iterators Marcin Kaliciński C++ 1 05-08-2005 09:58 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57