Jump to content
  • Advertisement
Sign in to follow this  
sfx81

how to use STL bind

This topic is 2507 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi All,
I am trying to understand and use stl bind2nd, but getting compiler error. Could some one please identify as what I am doing wrong.
Any help will be appreciated.
Thanks Kaz.

#include <functional>
#include <algorithm>
#include <vector>
int MyIntCounter = 0;
class MyInt
{
int mId;
public:
MyInt():mId(++MyIntCounter){}
MyInt(int arg):mId(arg){}
bool operator()(const MyInt& rhs1 , const MyInt& rhs2)
{
return rhs1.mId == rhs2.mId == mId;
}
};
int _tmain(int argc, _TCHAR* argv[])
{
std::vector<MyInt> ints;
for(int i = 0; i < 10; ++i) ints.push_back( MyInt() );
MyInt finder2(2), finder3(3);
std::find_if( ints.begin(), ints.end(),
std::bind2nd(finder2, finder3 ) );
return 0;
}

Share this post


Link to post
Share on other sites
Advertisement
First off, your object needs to be derived from std::binary_function, if you expect std::bind1st/std::bind2nd to work. The binding functions use typedefs within the binary_function class that you either have to define yourself, or inherit from the binary_function class.

Working code:

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

int MyIntCounter = 0;

class MyInt : public std::binary_function<MyInt, MyInt, bool>
{
int mId;
public:
MyInt(): mId(++MyIntCounter) {}
MyInt(int arg) : mId(arg){}

bool operator()(const MyInt& rhs1 , const MyInt& rhs2) const
{
return rhs1.mId == rhs2.mId == mId;
}
};

int main(int argc, char* argv[])
{
std::vector<MyInt> ints;
for(int i = 0; i < 10; ++i) ints.push_back( MyInt() );
MyInt finder2(2), finder3(3);
std::find_if( ints.begin(), ints.end(), std::bind2nd(finder2, finder3 ) );
return 0;
}

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!