473,657 Members | 2,659 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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::oper ator() 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_stri ng<char> string;
typedef std::vector<str ing> 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<str ing_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_it erator<string> (std::cout, "\n"));

return 1;
}
May 6 '06 #1
1 5801
* 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::oper ator() 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_stri ng<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<str ing> 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<str ing_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_it erator<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_func tion<string_vec tor, 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<str ing_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_it erator<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
2675
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 cardsAvailable without writing a loop. I have tried a number of different variations using the for_each algorithm without success, and am currently getting a C2664 error that the compiler cannot convert parameter 1. I am new to STL and not all that adept...
3
1769
by: John Black | last post by:
I have the following code trying to use bind1st, class C1{ ... }; class C2{ ... };
235
11683
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 stay with C and still be able to produce Java byte code for platform independent apps. Also, old programs (with some tweaking) could be re-compiled and ported to the JVM. We have been developing such a tool over the last 2 years and currently...
2
2133
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 <iostream> #include <list> #include <algorithm>
5
1213
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
6813
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. vector::iterator it2 = std::find_if (v1.begin (), v1.end (), std::bind1st (equal_to (), 3)); is better than
18
7201
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 <bitset> using std::vector;
2
6553
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
2403
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); RandomGenerator<doubleg; for(it = data.begin(); it != data.end(); ++it) { *it = g.GetGaussian();
0
8385
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8303
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8821
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8502
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8602
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
4150
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4300
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2726
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1941
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.