473,408 Members | 2,405 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

remove_if and member function pointers

Hello all,

Sorry if this is long, but I wanted to be specific...

Can anyone shed any light on the following errors in the context of the
following example code:

cullMethod1(); // compiles, but not what we want - we cant access
m_Exclustions

cullMethod2(); // fine but long-winded and causes many array re-allocations

cullMethod3(); // doen't compile!

i.e. How do you pass a member function to std functions.

Many thanks,
Jon Rea
////////////////////////////////////////////////////
// Begin example code ..

#include <vector>
#include <algorithm>
#include <functional>

using namespace std;

class Bond
{
public:
Bond( int _i, int _j )
{
i = _i;
j = _j;
}
Bond()
{
i = rand();
j = rand();
}
int i;
int j;
};

class MyBase
{
public:
virtual void Setup()
{
int bigNumber = 10000;
for( int i = 0; i < bigNumber; i++ )
{
m_Bonds.push_back( Bond() ); // add lots of random "bonds"
}
}
protected:
std::vector<Bondm_Bonds;
};

bool mySpecialSelectionFunction( Bond& _b )
{
return _b.i == 47;
}

class MyDerived : public MyBase
{
public:
virtual void Setup()
{
MyBase::Setup();

m_Exclustions.push_back(Bond(43,21));
m_Exclustions.push_back(Bond(12,14));
m_Exclustions.push_back(Bond(1,41));

// Remove all instances of the definitions in m_Exclustions from the
base class list.

// Call ONE of these in real code:
cullMethod1(); // compiles, but not what we want - we cant access
m_Exclustions
cullMethod2(); // fine but long-whinded and causes many array
re-allocations
cullMethod3(); // doen't compile!
}

bool Matches( Bond& bond )
{
for( size_t k = 0; k < m_Exclustions.size(); k++ )
{
if( ( bond.i == m_Exclustions[k].i && bond.j == m_Exclustions[k].j ) ||
( bond.j == m_Exclustions[k].i && bond.i == m_Exclustions[k].j ) )
{
return true;
}
}
return false;
}

void cullMethod1()
{
m_Bonds.erase( remove_if( m_Bonds.begin(), m_Bonds.end(),
mySpecialSelectionFunction ), m_Bonds.end() );
}

void cullMethod2()
{
for( size_t i = 0; i < m_Bonds.size(); /*blank*/ )
{
if( Matches( m_Bonds[i] ) )
{
m_Bonds.erase(m_Bonds.begin()+i);
}
else
{
i++;
}
}
}

void cullMethod3()
{
// Illegal operand on bound member function expression ...
//m_Bonds.erase( remove_if( m_Bonds.begin(), m_Bonds.end(), &Matches
), m_Bonds.end() );

// Term does not evaluare to a function taking 1 arguments ...
//m_Bonds.erase( remove_if( m_Bonds.begin(), m_Bonds.end(),
&MyDerived::Matches ), m_Bonds.end() );

// Term does not evaluare to a function taking 1 arguments ...
//m_Bonds.erase( remove_if( m_Bonds.begin(), m_Bonds.end(),
std::mem_fun_ref(&MyDerived::Matches) ), m_Bonds.end() );
}

protected:
std::vector<Bondm_Exclustions;
};

int _tmain(int argc, _TCHAR* argv[])
{
MyDerived bob;
bob.Setup();
return 0;
}
Nov 16 '06 #1
3 4127
In article <45**************@bris.ac.uk>, Jon Rea <jo*****@bris.ac.uk>
wrote:
Hello all,

Sorry if this is long, but I wanted to be specific...

Can anyone shed any light on the following errors in the context of the
following example code:

cullMethod1(); // compiles, but not what we want - we cant access
m_Exclustions

cullMethod2(); // fine but long-winded and causes many array re-allocations

cullMethod3(); // doen't compile!

i.e. How do you pass a member function to std functions.
First change the signature for Matches:

bool Matches(Bond bond) const
{
...
}

Then you can:

void cullMethod1()
{
m_Bonds.erase(remove_if(m_Bonds.begin(), m_Bonds.end(),
bind1st(mem_fun(&MyDerived::Matches), this)), m_Bonds.end());
}

--
To send me email, put "sheltie" in the subject.
Nov 17 '06 #2
Daniel T. wrote:
In article <45**************@bris.ac.uk>, Jon Rea <jo*****@bris.ac.uk>
wrote:
>Hello all,

Sorry if this is long, but I wanted to be specific...

Can anyone shed any light on the following errors in the context of the
following example code:

cullMethod1(); // compiles, but not what we want - we cant access
m_Exclustions

cullMethod2(); // fine but long-winded and causes many array re-allocations

cullMethod3(); // doen't compile!

i.e. How do you pass a member function to std functions.

First change the signature for Matches:

bool Matches(Bond bond) const
{
...
}

Then you can:

void cullMethod1()
{
m_Bonds.erase(remove_if(m_Bonds.begin(), m_Bonds.end(),
bind1st(mem_fun(&MyDerived::Matches), this)), m_Bonds.end());
}
Thanks for the reply. Thats better!

What though if I had to use:
bool Matches(BIG_Bond& bond) const;
because copying a 'BIG_Bond' is an expensive operation.

Cheers,
Jon
Nov 17 '06 #3
In article <J8********@bath.ac.uk>, Jon Rea <jo*****@bris.ac.ukwrote:
Daniel T. wrote:
In article <45**************@bris.ac.uk>, Jon Rea <jo*****@bris.ac.uk>
wrote:
Hello all,

Sorry if this is long, but I wanted to be specific...

Can anyone shed any light on the following errors in the context of the
following example code:

cullMethod1(); // compiles, but not what we want - we cant access
m_Exclustions

cullMethod2(); // fine but long-winded and causes many array re-allocations

cullMethod3(); // doen't compile!

i.e. How do you pass a member function to std functions.
First change the signature for Matches:

bool Matches(Bond bond) const
{
...
}

Then you can:

void cullMethod1()
{
m_Bonds.erase(remove_if(m_Bonds.begin(), m_Bonds.end(),
bind1st(mem_fun(&MyDerived::Matches), this)), m_Bonds.end());
}

Thanks for the reply. Thats better!

What though if I had to use:
bool Matches(BIG_Bond& bond) const;
because copying a 'BIG_Bond' is an expensive operation.
Test first. I would not be surprised to find that the compiler optimized
the copy away if the Matches function doesn't modify the parameter in
any way.

However, if you absolutely *had* to use a const& the you can always
create your own selector and then use:

m_Bonds.erase(remove_if(m_Bonds.begin(), m_Bonds.end(),
selector(&MyDerived::Matches, this)), m_Bonds.end());

Before I show you what "selector" looks like, you may be interested in
how I developed it. I put the above code in as the interface I wished,
and then fixed the compile errors until I had something that ran. After
than, I substituted templated tyeps. QED

template < typename Ret, typename Tp, typename Arg >
struct selector_t : unary_function< Arg, Ret >
{
Ret (Tp::*mfn)(Arg) const;
const Tp* obj;

selector_t(Ret(Tp::*x)(Arg) const, const Tp* y): mfn(x), obj(y) { }

Ret operator()(Arg x) const {
return (obj->*mfn)(x);
}
};

template < typename Ret, typename Tp, typename Arg >
selector_t< Ret, Tp, Arg selector(Ret(Tp::*x)(Arg) const, Tp* y) {
return selector_t< Ret, Tp, Arg >(x, y);
}

And yes, I double checked. Arg in the above template is a "const Bond&"
and not a "Bond".

--
To send me email, put "sheltie" in the subject.
Nov 18 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Newsgroup - Ann | last post by:
Gurus, I have the following implementation of a member function: class A { // ... virtual double func(double v); void caller(int i, int j, double (* callee)(double)); void foo() {caller(1,...
1
by: Paavo K | last post by:
Hi, can anyone tell what is wrong with following code : //..Code class removeTest { public: bool operator()(map<int, string>::value_type &d) const { // Just returning false to simplify...
2
by: marco_segurini | last post by:
Hi, if I compile the following code I get the error: RemoveIf.cpp(19) : error C2914: 'remove_if' : cannot deduce template argument as function argument is ambiguous #include <algorithm>...
2
by: joe | last post by:
hi, after reading some articles and faq, i want to clarify myself what's correct(conform to standard) and what's not? or what should be correct but it isn't simply because compilers don't...
37
by: Ben | last post by:
Hi, there. Recently I was working on a problem where we want to save generic closures in a data structure (a vector). The closure should work for any data type and any method with pre-defined...
11
by: cps | last post by:
Hi, I'm a C programmer taking my first steps into the world of C++. I'm currently developing a C++ 3D graphics application using GLUT (OpenGL Utility Toolkit written in C) for the GUI...
1
by: pheres | last post by:
Hi, I'm trying to pass pointers to member functions around in my code. The test with pointers to non-member function works fine: Code: void callOut( void (*callback)() ) { callback();
4
by: Josefo | last post by:
Hello, is someone so kind to tell me why I am getting the following errors ? vector_static_function.c:20: error: expected constructor, destructor, or type conversion before '.' token...
3
by: Angus | last post by:
Hi I have a class CRequest with a function: bool IsFinished() const; I have a list of these CRequests - list<CRequestmylist. I create a function object to find if a CRequest is finished:...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.