Problem binding a predicate to random_shuffle()

N

Nelis Franken

Good day. Thanks for the previous help on binding member functions to
use as predicates for STL functions (original example applied to
sort()). The technique to use Boost's bind() works well, except when
used with STL's random_shuffle(). The following code demonstrates a
working call (on sort()) and a call that generates an error (on
random_shuffle()), both using the same technique.

The code:

#include <algorithm>
#include <functional>
#include <vector>
#include <boost/bind.hpp>

using namespace std;

class Foo {

public:

Foo() {

for (int i=0; i < 5; i++) {
dataVector.push_back(i*10);
idVector.push_back(i);
}
}

bool isLess(int leftID, int rightID) {
return (dataVector[leftID] < dataVector[rightID]);
}

int customRand(int n) {
return (rand() % n);
}

void mySort() {
sort(idVector.begin(), idVector.end(),
boost::bind(&Foo::isLess, this, _1, _2));
}

void myShuffle() {
random_shuffle(dataVector.begin(), dataVector.end(),
boost::bind(&Foo::customRand, this, _1));
}

private:
vector<int> dataVector;
vector<int> idVector;

};

int main(int argc, char** argv) {

Foo myObj;

myObj.myShuffle();
myObj.mySort();

return 0;
}


The error:

sort.cpp: In member function `void Foo::myShuffle()':
sort.cpp:33: error: no matching function for call to
`random_shuffle(__gnu_cxx::__normal_iterator<int*, std::vector<int,
std::allocator<int> > >, __gnu_cxx::__normal_iterator<int*,
std::vector<int, std::allocator<int> > >, boost::_bi::bind_t<int,
boost::_mfi::mf1<int, Foo, int>,
boost::_bi::list2<boost::_bi::value<Foo*>, boost::arg<1> > >)'
/usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/bits/stl_algo.h:1766:
note: candidates are: void std::random_shuffle(_RandomAccessIterator,
_RandomAccessIterator, _RandomNumberGenerator&) [with
_RandomAccessIterator = __gnu_cxx::__normal_iterator<int*,
std::vector<int, std::allocator<int> > >, _RandomNumberGenerator =
boost::_bi::bind_t<int, boost::_mfi::mf1<int, Foo, int>,
boost::_bi::list2<boost::_bi::value<Foo*>, boost::arg<1> > >]

The code compiles and runs fine using VC7.1 (Visual Studio 2003), but
gives an error using g++ (GCC) 3.4.4 (cygming special). Any help will
be appreciated, once again.
 
V

Victor Bazarov

Nelis said:
Good day. Thanks for the previous help [...]

The code:

#include <algorithm>
#include <functional>
#include <vector>
#include <boost/bind.hpp>

[..]

Just to let you know, 'Boost' has its own online forums where you can
find help if your program uses it. At this point it's a third-party
library and is off-topic here, for the most part.

The reason it's off-topic is that not many people in this newsgroup are
necessarily the users of Boost library and any question related to it
is not of any iterest to them. Just like the use of Windows API or MFC
or GNU Numerics or OpenGL or ...

V
 
N

Nelis Franken

My apologies for posting off-topic. I wasn't sure if it would qualify,
as it had a 50% STL, 50% Boost breakdown in the problem. I have since
forwarded the question to the Boost mailing list, and I post the
solution provided by Jens Theisen here for completeness:

<snip>

The problem is that the temporary object returned by boost::bind
doesn't bind to the non-const reference random_shuffle accepts.

I'm not sure if there is a nicer solution to using boost::function, as
the return type of boost::bind is unspecified.

#include <boost/function.hpp>

void myShuffle() {
boost::function< int (int) > rand = boost::bind(&Foo::customRand,
this, _1);

random_shuffle(dataVector.begin(), dataVector.end(), rand);
}

</snip>

The above works perfectly. Thanks again for everyone's help.


Victor said:
Nelis said:
Good day. Thanks for the previous help [...]

The code:

#include <algorithm>
#include <functional>
#include <vector>
#include <boost/bind.hpp>

[..]

Just to let you know, 'Boost' has its own online forums where you can
find help if your program uses it. At this point it's a third-party
library and is off-topic here, for the most part.

The reason it's off-topic is that not many people in this newsgroup are
necessarily the users of Boost library and any question related to it
is not of any iterest to them. Just like the use of Windows API or MFC
or GNU Numerics or OpenGL or ...

V
 

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

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top