473,406 Members | 2,352 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,406 software developers and data experts.

std::bind1st making life difficult

I'm having trouble using bind1st with mem_fun1. I can't compile
the line marked below with a @@. If I use the join2 method everything
is fine, but I wind up passing string vectors by value. I would like
to use the reference passing version, or something similar.

The errors I get point the finger at binder1st::operator() being defined
as "const argument_type&" which becomes "const const string_vector&&"
when i use Joiner::join below.

Any advice would be appreciated. I use vc7.1

#include <string>
#include <vector>
#include <functional>
#include <algorithm>
#include <numeric>
#include <iostream>

typedef std::basic_string<char> string;
typedef std::vector<string> string_vector;

struct Joiner {
string join (const string_vector& v) {
return std::accumulate (v.begin(), v.end(), string()); }
string join2 (string_vector v) {
return std::accumulate (v.begin(), v.end(), string()); }
};

int main (int argc, char** argv)
{
// a 10x7 matrix of strings initialized to "Text"
std::vector<string_vector> vv (10, string_vector (7, "Text"));

Joiner j;
string_vector result (vv.size());
std::transform (vv.begin(), vv.end(),
result.begin(),
//std::bind1st (std::mem_fun1 (&Joiner::join), &j)); //error
std::bind1st (std::mem_fun1 (&Joiner::join2), &j));

std::copy (result.begin(), result.end(),
std::ostream_iterator<string> (std::cout, "\n"));

return 1;
}
May 6 '06 #1
1 5794
* pillbug:
I'm having trouble using bind1st with mem_fun1. I can't compile
the line marked below with a @@. If I use the join2 method everything
is fine, but I wind up passing string vectors by value. I would like
to use the reference passing version, or something similar.

The errors I get point the finger at binder1st::operator() being defined
as "const argument_type&" which becomes "const const string_vector&&"
when i use Joiner::join below.

Any advice would be appreciated. I use vc7.1

#include <string>
#include <vector>
#include <functional>
#include <algorithm>
#include <numeric>
#include <iostream>
Pedantic: #include <ostream> here (not that your compiler will complain).

typedef std::basic_string<char> string;
Please don't define a name that's used by the standard library; it only
sows confusion, and acts as a bug-attractor.

typedef std::vector<string> string_vector;
Good.

struct Joiner {
string join (const string_vector& v) {
return std::accumulate (v.begin(), v.end(), string()); }
string join2 (string_vector v) {
return std::accumulate (v.begin(), v.end(), string()); }
};
Is there perhaps some reason why these functions are non-const member
functions? I'll assume there is. But in the context of the code you've
shown, there's none: they should at least be const, preferably static,
or perhaps non-member.

int main (int argc, char** argv)
{
// a 10x7 matrix of strings initialized to "Text"
std::vector<string_vector> vv (10, string_vector (7, "Text"));

Joiner j;
string_vector result (vv.size());
std::transform (vv.begin(), vv.end(),
result.begin(),
//std::bind1st (std::mem_fun1 (&Joiner::join), &j)); //error
std::bind1st (std::mem_fun1 (&Joiner::join2), &j));

std::copy (result.begin(), result.end(),
std::ostream_iterator<string> (std::cout, "\n"));

return 1;
That's an undefined return value, but in practice it maps to "error".
You'd want to indicate "success". Do that by omitting return, or by
returning 0 (which is the default for main), or EXIT_SUCCESS.
}

Regarding the error, try

class JoinerFunc: public std::unary_function<string_vector, string>
{
private:
Joiner* myJ;
public:
JoinerFunc( Joiner* j ): myJ( j ) {}
string operator()( string_vector const& v ) const
{ return myJ->join( v ); }
};

int main()
{
// a 10x7 matrix of strings initialized to "Text"
std::vector<string_vector> vv (10, string_vector (7, "Text"));

Joiner j;
string_vector result (vv.size());
std::transform(
vv.begin(), vv.end(), result.begin(), JoinerFunc( &j )
);

std::copy (result.begin(), result.end(),
std::ostream_iterator<string> (std::cout, "\n"));
}
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
May 6 '06 #2

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

Similar topics

6
by: Greg Lilley | last post by:
I have an application where I want to remove all of the items that are in one vector from a second vector. In the following short program, my objective is to remove all of ourCards from...
3
by: John Black | last post by:
I have the following code trying to use bind1st, class C1{ ... }; class C2{ ... };
235
by: napi | last post by:
I think you would agree with me that a C compiler that directly produces Java Byte Code to be run on any JVM is something that is missing to software programmers so far. With such a tool one could...
2
by: Alberto | last post by:
Hello, while writing a program I ran across the problem of using for_each. Although I can traverse lists with a for loop, I'd prefer to use STL's for_each. Here's my faulty code: #include...
5
by: Nick Keighley | last post by:
Hi, In the beginning the code was:- BRUSHH new_brush = create_brush() BRUSHH old_brush = select_brush (new_brush) draw(...) select_brush (old_brush) destroy_brush (new_brush)
5
by: silverburgh.meryl | last post by:
I am reading an bind1st example from http://www.roguewave.com/support/docs/sourcepro/stdlibref/bind1st.html Can someone please tell me why // Even better, construct the new predicate on the fly....
18
by: ma740988 | last post by:
Trying to get more acclimated with the use of function objects. As part of my test, consider: # include <vector> # include <iostream> # include <algorithm> #include <stdexcept> #include...
2
by: Alex | last post by:
I don't know why this works: class C { public: C(int x, int y) : a(x), b(y) {} int a,b; }; float avg(const C* c) {
3
by: Bill Woessner | last post by:
I'm trying to replace a loop with a call to std::generate and I'm about at wits end. Here's the working code: std::vector<double>::iterator it; std::vector<doubledata(100);...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.