Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Can I use accumulate to do this?

Reply
Thread Tools

Can I use accumulate to do this?

 
 
Suman
Guest
Posts: n/a
 
      03-07-2009
I just can't get my head around to create a suitable
binary_function that will do this i.e. call a member function
from within accumulate. In fact I rolled my own template.
But I have a terrible feeling of reinventing the wheel.

/
*----------------------------------------------------------------------
*/
#include <vector>
#include <iterator>
#include <iostream>

using namespace std;

template <class I, class V, class Fn1, class Fn2>
V accumulate2(I first, I last, V val, Fn1 op, Fn2 memfn) {
for (; first != last; ++first)
val = op(val, memfn(*first));
return val;
}

struct strange {
strange(int a, int b) : _a(a), _b(b) {}
int value() { return _a + 10 * _b; }
int _a, _b;
};

int main() {
std::vector<strange> dv;
dv.push_back(strange(1, 3));
dv.push_back(strange(4, 6));
dv.push_back(strange(20, -11));
cout << accumulate2(dv.begin(), dv.end(),
0, std:lus<int>(),
mem_fun_ref(&strange::value))
<< endl;
}

/
*----------------------------------------------------------------------
*/

I have not yet looked into Boost -- but suggestions
are most welcome.

Regards,
Suman
 
Reply With Quote
 
 
 
 
joecook@gmail.com
Guest
Posts: n/a
 
      03-08-2009
On Mar 7, 12:42*pm, Suman <skar...@gmail.com> wrote:
> I just can't get my head around to create a suitable
> binary_function that will do this i.e. call a member function
> from within accumulate. In fact I rolled my own template.
> But I have a terrible feeling of reinventing the wheel.


It looks like you are really trying to call two functions? Take a
look at std::inner_product.

HTH,
Joe
 
Reply With Quote
 
 
 
 
Martin Eisenberg
Guest
Posts: n/a
 
      03-08-2009
Suman wrote:

> I just can't get my head around to create a suitable
> binary_function that will do this i.e. call a member function
> from within accumulate. In fact I rolled my own template.
> But I have a terrible feeling of reinventing the wheel.


The basic version -- note that I made strange::value() const:

#include <iostream>
#include <vector>
#include <numeric>

struct strange {
strange(int a, int b) : _a(a), _b(b) {}
int value() const { return _a + 10 * _b; }
int _a, _b;
};

struct AddValue {
int operator()(int x, const strange& y) { return x + y.value(); }
};

int main()
{
std::vector<strange> dv;
dv.push_back(strange(1, 3));
dv.push_back(strange(4, 6));
dv.push_back(strange(20, -11));

std::cout << std::accumulate(dv.begin(), dv.end(), 0, AddValue());
std::cout << std::endl;
return 0;
}

And the flashy version:

#include <boost/lambda/lambda.hpp>
#include <boost/lambda/bind.hpp>

//...

using namespace boost::lambda;
std::cout << std::accumulate(dv.begin(), dv.end(), 0,
_1 + bind(&strange::value, _2));


Martin

--
Quidquid latine scriptum est, altum videtur.
 
Reply With Quote
 
Suman
Guest
Posts: n/a
 
      03-08-2009


joec...@gmail.com wrote:

> On Mar 7, 12:42*pm, Suman <skar...@gmail.com> wrote:
> > I just can't get my head around to create a suitable
> > binary_function that will do this i.e. call a member function
> > from within accumulate. In fact I rolled my own template.
> > But I have a terrible feeling of reinventing the wheel.

>
> It looks like you are really trying to call two functions? Take a
> look at std::inner_product.
>


I'm not sure I understand. I only have a single vector of `strange`
objects. Maybe, you can help me out with a line of code. Note, I
really wanted to be able to work my way out with accumulate. Of
course, a new trick or two will never go in waster

Regards,
Suman
 
Reply With Quote
 
dimwit
Guest
Posts: n/a
 
      03-08-2009
On Mar 8, 7:19*pm, Martin Eisenberg <martin.eisenb...@udo.edu> wrote:
[...]
>
> The basic version -- note that I made strange::value() const:


That's a good thing to do -- I missed it.

[...]
> struct AddValue {
> * int operator()(int x, const strange& y) *{ return x + y.value(); }
>
> };
>


Thanks! I should have mentioned that I already know how
to pass in functors. I was thinking about creating functors
on-the-fly much the way boost does, without using boost. I
ended up realising that bind curries and I'd need something
more powerful to be able to compose functions.

>
> And the flashy version:

[...]
> * std::cout << std::accumulate(dv.begin(), dv.end(), 0,
> * * * * * * * * * * * * * * * *_1 + bind(&strange::value, _2));
>


I spent the entire afternoon reading up BLL. I came up with
a slightly heavier version:

std::cout << std::accumulate(dv.begin(), dv.end(), 0,
_1 += bind(&strange::value, _2));

I'll dive deeper and see what goes on under the hoods.

Regards,
Suman
 
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
How to use Boost bind library with STL accumulate algorithm yinglcs@gmail.com C++ 1 02-15-2006 11:03 AM
Using accumulate algorithm with a set of pairs cesco C++ 5 02-13-2006 03:12 PM
accumulate() usage Allerdyce.John@gmail.com C++ 1 02-02-2006 06:30 AM
Accumulate 1.0 Roedy Green Java 0 07-17-2004 03:58 AM
64 bit add and accumulate with MMX Brian K. Michalk Perl 6 09-20-2003 08:43 PM



Advertisments