anything wrong calling bind2nd this way?

J

John Black

Hi,
I have some code like this,

itr = find_if(this->pool.begin(), this->pool.end(),
bind2nd(mem_fun_ref(&Pool::isAvailable),
make_pair(base, high)));

But compiler always complains a lot STL template instantiation
errors.
My class definition is like this,

class Pool{
public:
vector<pair<int, int> > pool;

bool isAvailable(pair<int, int>);

void func();
};

and the above find_if is a statement with Pool::func().


Thanks.
 
L

Leor Zolman

Hi,
I have some code like this,

itr = find_if(this->pool.begin(), this->pool.end(),
bind2nd(mem_fun_ref(&Pool::isAvailable),
make_pair(base, high)));

But compiler always complains a lot STL template instantiation
errors.
My class definition is like this,

class Pool{
public:
vector<pair<int, int> > pool;

bool isAvailable(pair<int, int>);

void func();
};

and the above find_if is a statement with Pool::func().

Okay, this one took me /way/ too long to figure out. It is one of those
optical-illusion types of errors ;-)

Think about what you're invoking your call to find_if upon: a vector of
pairs. NOT a vector of "Pool" objects. Yet you're setting the find_if call
up as if each element it'll be iterating over is a Pool (i.e., you're
trying to have the algorithm invoke a member function on each element of
the vector.) But the vector's elements aren't Pools, they're std::pairs.
What you probably wanted was something as simple as this:

itr = find(pool.begin(), pool.end(), make_pair(base, high));

HTH,
-leor
 
J

John Black

Leor said:
Okay, this one took me /way/ too long to figure out. It is one of those
optical-illusion types of errors ;-)

Think about what you're invoking your call to find_if upon: a vector of
pairs. NOT a vector of "Pool" objects. Yet you're setting the find_if call
up as if each element it'll be iterating over is a Pool (i.e., you're
trying to have the algorithm invoke a member function on each element of
the vector.) But the vector's elements aren't Pools, they're std::pairs.
What you probably wanted was something as simple as this:

itr = find(pool.begin(), pool.end(), make_pair(base, high));

HTH,
-leor

Leor,
Thanks for pointing the hierarchy error.

But the problem is I must use a function as search condition, actually the
reason is simple, this is how I implement isAvailable():

if (...this->pool has a pair which 'cover' the given pair... ) {
return true;
}

Here one pair 'cover' another means that this pair's first is less and
equals than the other's first and its second is large and equals to the
other's second.

Any suggestions?

Regards.
 
D

Dave Moore

John Black said:
But the problem is I must use a function as search condition, actually the
reason is simple, this is how I implement isAvailable():

if (...this->pool has a pair which 'cover' the given pair... ) {
return true;
}

Here one pair 'cover' another means that this pair's first is less and
equals than the other's first and its second is large and equals to the
other's second.

Any suggestions?
Well .. it seems there may be two levels of iteration here ..
otherwise I don't understand why you need std::find_if. Your stated
implementation of isAvailable seems to be mimicking the operation of
std::find_if anyway.

So, if all you want to do is search a vector of pair<int,int> (inside
a Pool object) for the first instance satisfying your "cover"
conditions, then just write something like:

// Note that I have added std:: where you omitted it and also made
some
// necessary assumptions to answer your question. If my assumptions
are
// wrong, re-post with some more details and I will work from there

bool covers(const std::pair<int,int>& a, const std::pair<int,int>& b)
{
return a.first<=b.first && a.second>=b.second;
}

void func(Pool &pool, int base, int high) {
// I am assuming here that Pool has the appropriate typedef's and
forwarding
// functions for the following code to work

Pool::iterator itr = std::find_if(pool.begin(), pool.end(),
std::bind2nd(std::ptr_fun(covers),

std::make_pair(base,high)));

// rest of function using result
}

If there is another level of "finding" that needs to be done, you can
post some more details and I will try to help you more.

HTH, Dave Moore
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top