By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,413 Members | 1,023 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,413 IT Pros & Developers. It's quick & easy.

anything wrong calling bind2nd this way?

P: n/a
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.

Jul 22 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
On Tue, 01 Jun 2004 19:27:27 -0600, John Black <bl***@eed.com> wrote:
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
Thanks.


--
Leor Zolman --- BD Software --- www.bdsoft.com
On-Site Training in C/C++, Java, Perl and Unix
C++ users: download BD Software's free STL Error Message Decryptor at:
www.bdsoft.com/tools/stlfilt.html
Jul 22 '05 #2

P: n/a
Leor Zolman wrote:
On Tue, 01 Jun 2004 19:27:27 -0600, John Black <bl***@eed.com> wrote:
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
Thanks.


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.

Jul 22 '05 #3

P: n/a
John Black <bl***@eed.com> wrote in message news:<40***************@eed.com>...
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
Jul 22 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.