Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > std::bind1st making life difficult

Reply
Thread Tools

std::bind1st making life difficult

 
 
pillbug
Guest
Posts: n/a
 
      05-06-2006
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:perator() 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:stream_iterator<string> (std::cout, "\n"));

return 1;
}
 
Reply With Quote
 
 
 
 
Alf P. Steinbach
Guest
Posts: n/a
 
      05-06-2006
* 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:perator() 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:stream_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:stream_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?
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Making a tic tac toe game difficult to win shadow427 Java 19 11-18-2005 05:26 PM
RAII- Am I making life difficult? [LONGISH] Nick Keighley C++ 5 08-03-2005 11:31 PM
making easy things difficult: @a-@b wana Perl Misc 9 10-03-2004 04:42 PM
Making DVD's from VHS difficult?? FERRANTE Computer Information 1 02-01-2004 05:13 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57