473,480 Members | 2,230 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

std::generate and Function Adaptors

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();
}

This works fine. So I thought I could use std::generate to accomplish
the same thing. I've tried lots of combinations, but this is the one
I thought most likely to work:

std::generate(data.begin(), data.end(),
std::bind1st(std::mem_fun(&RandomGenerator<double> ::GetGaussian),
&g));

I thought this should work because GetGaussian effectively has one
parameter, namely the object it's being invoked on. However, when I
try to compile this, I get a slew of errors about first_argument_type
and second_argument_type not being defined.

Any thoughts?

Thanks in advance,
Bill

Jun 27 '07 #1
3 2388
Bill Woessner wrote:
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();
}

This works fine. So I thought I could use std::generate to accomplish
the same thing. I've tried lots of combinations, but this is the one
I thought most likely to work:

std::generate(data.begin(), data.end(),
std::bind1st(std::mem_fun(&RandomGenerator<double> ::GetGaussian),
&g));

I thought this should work because GetGaussian effectively has one
parameter, namely the object it's being invoked on. However, when I
try to compile this, I get a slew of errors about first_argument_type
and second_argument_type not being defined.

Any thoughts?

Thanks in advance,
Bill
because you don't need bind1st, try
std::generate(data.begin(), data.end(),
std::mem_fun(&RandomGenerator<double>::GetGaussian ));
Jun 27 '07 #2
Bill Woessner wrote:
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();
}

This works fine. So I thought I could use std::generate to accomplish
the same thing. I've tried lots of combinations, but this is the one
I thought most likely to work:

std::generate(data.begin(), data.end(),
std::bind1st(std::mem_fun(&RandomGenerator<double> ::GetGaussian),
&g));

I thought this should work because GetGaussian effectively has one
parameter, namely the object it's being invoked on. However, when I
try to compile this, I get a slew of errors about first_argument_type
and second_argument_type not being defined.

Any thoughts?
'binder1st' inherits from 'unary_function' accepting the second argument
of the function it binds. Since in your case the second argument doesn't
exist (it's 'void'), 'mem_fun' does not provide it, so it becomes missing
AFA 'binder1st' is concerned...

You would be better off using advanced binders from Boost, or providing
a simple operator() instead of 'GetGaussian' (or wrapping 'GetGaussian'
yourself).

template<class Tstruct RandomGenerator {
T GetGaussian() const { return 42.; }
// other methods
};

template<class Tstruct GaussianWrapper_t {
RandomGenerator<Tg;
GaussianWrapper_t(RandomGenerator<Tconst& g) : g(g) {}
T operator()() const {
return g.GetGaussian();
}
};

template<class T>
GaussianWrapper_t<TGaussianWrapper(RandomGenerator <Tconst& g)
{
return GaussianWrapper_t<T>(g);
}

int main()
{
std::vector<double>::iterator it;
std::vector<doubledata(100);
RandomGenerator<doubleg;
std::generate(data.begin(), data.end(), GaussianWrapper(g));
}

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jun 27 '07 #3
Fei Liu wrote:
Bill Woessner wrote:
>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();
}

This works fine. So I thought I could use std::generate to
accomplish the same thing. I've tried lots of combinations, but
this is the one I thought most likely to work:

std::generate(data.begin(), data.end(),
std::bind1st(std::mem_fun(&RandomGenerator<double >::GetGaussian),
&g));

I thought this should work because GetGaussian effectively has one
parameter, namely the object it's being invoked on. However, when I
try to compile this, I get a slew of errors about first_argument_type
and second_argument_type not being defined.

Any thoughts?

Thanks in advance,
Bill
because you don't need bind1st, try
std::generate(data.begin(), data.end(),
std::mem_fun(&RandomGenerator<double>::GetGaussian ));
How is 'generate' going to know what object to call it for?

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jun 27 '07 #4

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

Similar topics

3
3945
by: Tanguy Fautré | last post by:
Hi, given a class class foo { private: void A(); type B(); };
5
3399
by: cppaddict | last post by:
Hi, I'm confused about what the comparison operator in a map template is: In particular, I want to know why something like the following doesn't work: bool pointCompare(POINT p1, POINT p2)...
10
5021
by: JKop | last post by:
An excerpt from the Standard: 4 Calling the function void exit(int); declared in <cstdlib> (18.3) terminates the program without leaving the current block and hence without destroying any...
8
4208
by: Manfred | last post by:
Hello I am new to template programming, so i tried the 'example' from http://www.sgi.com/tech/stl/functors.html. I can compile the code but when i want to run the program I get a segmentation...
5
1828
by: Chameleon | last post by:
I totally messed up with this: We have -------------------------------------- generate(v.begin(), v.end(), my_func); -------------------------------------- and my_func: ----------- int...
8
2452
by: Klaas Vantournhout | last post by:
Hi all, I'm in need of a matrix of function pointers, and to be honest. No 'nice' solution has been found yet on that big big internet. It is possible to declare a matrix of function pointers...
7
6600
by: Lighter | last post by:
Is overriding a function of a library in accordance with C++ standard? The following code are passed by the VS 2005 and Dev C++. #include <cstdlib> #include <iostream> using namespace std;...
2
2028
by: psujkov | last post by:
Hi everybody, does anyone knows is there any way to have an implicit iteration counter in std::for_each ? e.g. std::for_each(c.begin(), c.end(), boost::bind(&C::set_id, _1,...
58
4769
by: Mark Casternoff | last post by:
I'm getting back into C++ after a long hiatus (they didn't have namespaces back then). I know this question is completely subjective, but I'd be interested in hearing which is the "better"...
0
7052
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
6981
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...
1
4790
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...
0
4488
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3000
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...
0
2989
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1304
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 ...
1
565
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
188
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.