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.
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.