For Programmers: Free Programming Magazines  


Home > Archive > VC STL > February 2005 > predicates and state









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author predicates and state
Stephen Howe

2005-02-14, 9:11 pm

Given std::vector<int> that is sorted

and contains duplicates so that we may have

-1, -1, -1, 2, 5, 6, 6, 6, 7

I would like to count the number of distinct group of ints.
I would use count_if() with an approprate predicate but predicates are not
supposed to record changing state (like what was the last integer seen). How
else can I do this?

Thanks

Stephen Howe


Pete Becker

2005-02-14, 9:11 pm

Stephen Howe wrote:
> Given std::vector<int> that is sorted
>
> and contains duplicates so that we may have
>
> -1, -1, -1, 2, 5, 6, 6, 6, 7
>
> I would like to count the number of distinct group of ints.
> I would use count_if() with an approprate predicate but predicates are not
> supposed to record changing state (like what was the last integer seen). How
> else can I do this?
>


That's too broad. The true rule is that predicates can be copied
somewhat arbitrarily, so you can't reliably store state in an individual
object. What you can do is store state in a way that isn't affected by
copying, typically with a pointer or reference to something that doesn't
get copied, such as an object on the stack or the heap, or a static data
member.

But offhand I'd say that that's doing it the hard way. How about writing
an algorithm? Untested code:

template<class FwdIt>
unsigned count_groups(FwdIt begin, FwdIt end)
{
unsigned count = 0;
if (begin != end)
{
iterator_traits<FwdIt>::value_type cur = *begin++;
while (begin != end)
{
if (*begin != cur)
{
++count;
cur = *begin;
}
++begin;
}
}
return count;
}

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
Priyesh

2005-02-14, 9:11 pm


"Stephen Howe" <stephenPOINThoweATtns-globalPOINTcom> wrote in message
news:uY45w3TCFHA.1924@TK2MSFTNGP14.phx.gbl...
> Given std::vector<int> that is sorted
>
> and contains duplicates so that we may have
>
> -1, -1, -1, 2, 5, 6, 6, 6, 7
>
> I would like to count the number of distinct group of ints.
> I would use count_if() with an approprate predicate but predicates are not
> supposed to record changing state (like what was the last integer seen).

How
> else can I do this?
>
> Thanks
>
> Stephen Howe
>
>


Would a multimap be appropriate in this situation?


Stephen Howe

2005-02-14, 9:11 pm

> But offhand I'd say that that's doing it the hard way. How about writing
> an algorithm? Untested code:
>
> template<class FwdIt>
> unsigned count_groups(FwdIt begin, FwdIt end)
> {
> unsigned count = 0;
> if (begin != end)
> {
> iterator_traits<FwdIt>::value_type cur = *begin++;
> while (begin != end)
> {
> if (*begin != cur)
> {
> ++count;
> cur = *begin;
> }
> ++begin;
> }
> }
> return count;
> }


Thanks. You are right. It is shame that these are not part of the standard.
It is analogous to

SELECT COUNT(field) FROM table1
SELECT COUNT(DISTINCT field) FROM table1

for SQL. That would suggest

count_group()
count_group_if()

Stephen


Stephen Howe

2005-02-14, 9:11 pm

>
> Would a multimap be appropriate in this situation?


Thanks. It is an idea but I don't think it will help.

Stephen Howe


Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com