count algorithm 
Author Message
 count algorithm

Hello,

I have a std::list of struct *.  One element of the struct indicates what
flavor of thing it is.  I'd like to get a count of how many of one flavor is
in the list.  Currently, I have:
  RoadListItr itr = m_RoadList.begin();
  while (itr != m_RoadList.end()) {
   if ((*itr)->m_type == CRoad::SECONDARY)  ++lCnt;
   ++itr;
  }

Is there a count function or algorithm that would encapsulate this better?
    count(itr, m_RoadList.end(), someComparisonFn)
for example?

TIA.
C. Bates



Mon, 17 May 2004 23:52:05 GMT  
 count algorithm


Quote:
> Hello,

> I have a std::list of struct *.  One element of the struct indicates what
> flavor of thing it is.  I'd like to get a count of how many of one flavor
is
> in the list.  Currently, I have:
>   RoadListItr itr = m_RoadList.begin();
>   while (itr != m_RoadList.end()) {
>    if ((*itr)->m_type == CRoad::SECONDARY)  ++lCnt;
>    ++itr;
>   }

> Is there a count function or algorithm that would encapsulate this better?
>     count(itr, m_RoadList.end(), someComparisonFn)
> for example?

std::count_if()

marco



Tue, 18 May 2004 01:48:47 GMT  
 count algorithm


Fri, 19 Jun 1992 00:00:00 GMT  
 count algorithm
Is there a way to pass a non-static member function as the Pred argument to
count_if()?
Thanks again...
Quote:
> std::count_if()



Tue, 18 May 2004 04:37:50 GMT  
 count algorithm

Quote:
> Is there a way to pass a non-static member function as the Pred argument
to
> count_if()?
> Thanks again...

> > std::count_if()

pass "bind1st(mem_fun(yourClass::yourFn),yourClassInstance)"

Ken



Tue, 18 May 2004 05:31:14 GMT  
 count algorithm

Quote:
> pass "bind1st(mem_fun(yourClass::yourFn),yourClassInstance)"

And "yourClassInstance" would be "this"? (And there would be another right
paren? )

Thanks for the reply.



Tue, 18 May 2004 07:47:33 GMT  
 count algorithm
  lCnt = std::count_if(itrStart, itrEnd,
std::bind1st(std::mem_fun(CBaseImpl::testROADTYPE), this));

.... doesn't work so well.  testROADTYPE() is a private member function.
Here's the error message:
error C2784: 'class std::mem_fun_t<_R,_Ty> __cdecl std::mem_fun(_R
(__thiscall _Ty::*)(void))' : could not deduce template argument for
'overloaded function type' from 'overloaded function type'

What does "yourClassInstance" refer to?

Quote:
> pass "bind1st(mem_fun(yourClass::yourFn),yourClassInstance)"

Thanks for your help.


Tue, 18 May 2004 08:00:02 GMT  
 count algorithm


Quote:
>   lCnt = std::count_if(itrStart, itrEnd,
> std::bind1st(std::mem_fun(CBaseImpl::testROADTYPE), this));

> .... doesn't work so well.  testROADTYPE() is a private member function.
> Here's the error message:
> error C2784: 'class std::mem_fun_t<_R,_Ty> __cdecl std::mem_fun(_R
> (__thiscall _Ty::*)(void))' : could not deduce template argument for
> 'overloaded function type' from 'overloaded function type'

> What does "yourClassInstance" refer to?

I didn't realize you were trying to call a parameterless function.  I assume
the objects you want to call testROADTYPE on are stored in your container
then?  If that is the case, then just pass

std::mem_fun(CBaseImpl::testROADTYPE)

yourClassInstance in my example was a class you wanted to call the member
function on, but since it seems you want to call it on the contents of your
container, you don't want to bind that parameter.

mem_fun converts a member function of N parameters into a non-member
function of N+1 parameters, where the first parameter is the object that
owns the member function.

Ken



Tue, 18 May 2004 13:27:47 GMT  
 count algorithm
Thanks for your help, Ken.  Maybe this will make what we're trying to talk
about clearer:

class someClass {
    std::list<someStruct *> m_List;

    int SomeMethod(/* some return param */) {

        std::list<someStruct *>::iterator itrStart =  m_List.begin();
        std::list<someStruct *>::iterator itrEnd =  m_List.end();
        long l = count_if(itrStart, itrEnd,
std::mem_fun(someClass::testROADTYPE));    // error here...
    }

    bool testROADTYPE(someStruct *) { return true; }

Quote:
};

Since testROADTYPE takes one argument, it seems I need bind1st...

Quote:
> I didn't realize you were trying to call a parameterless function.  I
assume
> the objects you want to call testROADTYPE on are stored in your container
> then?  If that is the case, then just pass

> std::mem_fun(CBaseImpl::testROADTYPE)

> yourClassInstance in my example was a class you wanted to call the member
> function on, but since it seems you want to call it on the contents of
your
> container, you don't want to bind that parameter.

> mem_fun converts a member function of N parameters into a non-member
> function of N+1 parameters, where the first parameter is the object that
> owns the member function.

> Ken



Tue, 18 May 2004 21:02:47 GMT  
 count algorithm

Quote:
> Thanks for your help, Ken.  Maybe this will make what we're trying to talk
> about clearer:

> class someClass {
>     std::list<someStruct *> m_List;

>     int SomeMethod(/* some return param */) {

>         std::list<someStruct *>::iterator itrStart =  m_List.begin();
>         std::list<someStruct *>::iterator itrEnd =  m_List.end();
>         long l = count_if(itrStart, itrEnd,
> std::mem_fun(someClass::testROADTYPE));    // error here...
>     }

>     bool testROADTYPE(someStruct *) { return true; }

> };

> Since testROADTYPE takes one argument, it seems I need bind1st...

OK, gotcha now.  I didn't realize you actually meant a member function of
the current class.  This should work for you (just compiled fine on my
machine):

std::count_if(itrStart, itrEnd,
std::bind1st(std::mem_fun(someClass::testROADTYPE),this));

Ken



Wed, 19 May 2004 06:03:43 GMT  
 count algorithm
What am I missing here?  I just tried to compile this:
/* ---------------------------------------------------- */
#include <tchar.h>
#include <list>
#include <algorithm>

struct someStruct {
 int i;

Quote:
};

class someClass {
    std::list<someStruct *> m_List;

    int SomeMethod(TCHAR * pOut) {

        std::list<someStruct *>::iterator itrStart =  m_List.begin();
        std::list<someStruct *>::iterator itrEnd =  m_List.end();
        long l = std::count_if(itrStart, itrEnd,
   std::bind1st(std::mem_fun(someClass::testROADTYPE),this));    // Still
error here
    }

    bool testROADTYPE(someStruct *) { return true; }

Quote:
};

/* ----------------------------------------------------------------- */
and got the error:
error C2784: 'class std::mem_fun_t<_R,_Ty> __cdecl std::mem_fun(_R
(__thiscall _Ty::*)(void))' : could not deduce template argument for
'overloaded function type' from 'overloaded function type'

It looks like mem_fun expects a function with no arguments?

Quote:
> OK, gotcha now.  I didn't realize you actually meant a member function of
> the current class.  This should work for you (just compiled fine on my
> machine):

> std::count_if(itrStart, itrEnd,
> std::bind1st(std::mem_fun(someClass::testROADTYPE),this));

> Ken



Wed, 19 May 2004 07:08:54 GMT  
 count algorithm

<snip>

Quote:
> and got the error:
> error C2784: 'class std::mem_fun_t<_R,_Ty> __cdecl std::mem_fun(_R
> (__thiscall _Ty::*)(void))' : could not deduce template argument for
> 'overloaded function type' from 'overloaded function type'

> It looks like mem_fun expects a function with no arguments?

Hmm...the exact code you posted compiles on my machine, however I have the
VC7 beta installed, I assume you are using VC6?  I don't have a copy of that
handy at the moment to test against, but here's a thought.  Try replacing
mem_fun with mem_fun1 to explicitly use the one parameter version.  It might
be having a hard time deducing the correct version.

Ken



Wed, 19 May 2004 15:03:42 GMT  
 
 [ 12 post ] 

 Relevant Pages 

1. Bit Counting Algorithms

2. Help! Bit counting algorithm...

3. How can i count numbers in an int or count characters in a string in the

4. Differencies in compilation: a[count++]=count;

5. Counting the clock... (counting clock cycles)

6. Algorithm of Counting Array Occurance in ANSI-C (URGENT!!) Many Thx!

7. count number in an int & count characters in a string

8. Counting bits set in 32 bit word - Population Count.

9. Form count

10. Page count for files

11. .NET Reference counting not working?

12. Ambiguous definition of Count on type safe collection (sample provided)

 

 
Powered by phpBB® Forum Software