473,385 Members | 1,958 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Problem binding a predicate to random_shuffle()

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<intdataVector;
vector<intidVector;

};

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.

Aug 21 '06 #1
2 3267
Nelis Franken wrote:
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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Aug 21 '06 #2
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 Bazarov wrote:
Nelis Franken wrote:
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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Aug 22 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Omar | last post by:
When I try to databind my comboBox (specifically field "emplcode") to a filled dataset , the contents of the comboBox displays a bunch of "System.Data.DataRowView". I assume the amount of times...
2
by: Josema | last post by:
Hi, I have a DropDownList called BSDropDown and the following code: this.BSDropDown.DataSource=B2BSS_; this.BSDropDown.DataTextField="Name"; this.BSDropDown.DataValueField="ID"; ListItem...
0
by: Michael Rodriguez | last post by:
Whidbey, BindingSource, Strongly-typed DataSets: If I bind a textbox to a sql column that is defined as "int", I can't wipe out the contents of the textbox! If I empty the box, VS won't let me...
2
by: Gastin | last post by:
I am consuming a web serivce from Amazon.Com. I have the following class which was autogenerated by VS.NET when I created a Web Reference to...
5
by: Vigneshwar Pilli via DotNetMonster.com | last post by:
string connectionString1 = "server=(local); user=sa;password=sa; database=sonic"; System.Data.SqlClient.SqlConnection dbConnection1 = new System.Data.SqlClient.SqlConnection(connectionString1);...
1
by: Laura K | last post by:
Can someone tell me where I went wrong. This is my code for binding data to a datalist. <script language="vb"> Sub Page_Load(Src As Object, e As EventArgs) Dim Con As New...
2
by: Rodney Lane | last post by:
Hi, I am using an oledb data adapter to connect to an MSAccess 2003 database. I use bindings on my windows forms to bind fields to the controls and for text boxes and grids it has been fine. ...
5
by: Poonam | last post by:
I have a department table with DeptID and DeptName. I am trying to fill my combobox with DeptName. Here is my code but it does not work? Can someone help me? Dim pxy As New Channel1.Channel...
1
by: final74 | last post by:
Hi I'm experimenting with PHP5, ORacle 9i and ADODB and have run into problems trying to bind and output variable. I can make a successful connection to our database via the MSDAORA provider...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.