Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > accumulate instead of for-loop

Reply
Thread Tools

accumulate instead of for-loop

 
 
Alex Vinokur
Guest
Posts: n/a
 
      04-03-2008

Let v be:

vector<pair<string, size_t> > v;

sum = 0;
for (size_t k = 0; k < v.size(); k++)
{
sum += v[k].second;
}


Is it possible to compute sum using std::accumulate (i.e. not using
for-loop)?



Alex Vinokur
email: alex DOT vinokur AT gmail DOT com
http://mathforum.org/library/view/10978.html
http://sourceforge.net/users/alexvn


 
Reply With Quote
 
 
 
 
Boris Glawe
Guest
Posts: n/a
 
      04-03-2008


http://www2.roguewave.com/support/do...ccumulate.html


greets Boris
 
Reply With Quote
 
 
 
 
James Kanze
Guest
Posts: n/a
 
      04-03-2008
On Apr 3, 1:24 pm, Alex Vinokur <(E-Mail Removed)> wrote:
> Let v be:


> vector<pair<string, size_t> > v;


> sum = 0;
> for (size_t k = 0; k < v.size(); k++)
> {
> sum += v[k].second;
> }


> Is it possible to compute sum using std::accumulate (i.e. not using
> for-loop)?


I'm pretty sure that Boost iterators have something which
supports this, probably right out of the box, but at the very
least transform_iteratror in collaboration with bind will do the
trick.

--
James Kanze (GABI Software) email:(E-Mail Removed)
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
 
Reply With Quote
 
Jerry Coffin
Guest
Posts: n/a
 
      04-03-2008
In article <21e0cb00-9ae3-4f87-805c-13c6679fe9a3
@i36g2000prf.googlegroups.com>, http://www.velocityreviews.com/forums/(E-Mail Removed) says...
>
> Let v be:
>
> vector<pair<string, size_t> > v;
>
> sum = 0;
> for (size_t k = 0; k < v.size(); k++)
> {
> sum += v[k].second;
> }
>
> Is it possible to compute sum using std::accumulate (i.e. not using
> for-loop)?


Yes, though it's open to question whether it's really any improvement:

typedef std:air<std::string, size_t> p;

struct add_second : std::binary_function<size_t, size_t, p> {
size_t operator()(size_t a, p const &b) { return a+b.second; }
};

size_t sum = std::accumulate(v.begin(), v.end(), 0, add_second());

Using Boost.lambda, I believe you should also be able to do something
like this:

size_t sum=std::accumulate(v.begin(), v.end(), 0, var(_1).second);

--
Later,
Jerry.

The universe is a figment of its own imagination.
 
Reply With Quote
 
Michael DOUBEZ
Guest
Posts: n/a
 
      04-03-2008
Alex Vinokur a écrit :
> Let v be:
>
> vector<pair<string, size_t> > v;
>
> sum = 0;
> for (size_t k = 0; k < v.size(); k++)
> {
> sum += v[k].second;
> }
>
>
> Is it possible to compute sum using std::accumulate (i.e. not using
> for-loop)?


You can use a functor to project on the second element of the pair:

template<class Pair>
class pair_get2nd
{
typedef Pair argument_type;
typedef typename Pair::second_type result_type;

const result_type& operator()(const argument_type& t)const
{
return t.second;
}
};

And then

typedef vector<pair<string, size_t> > my_vector;
my_vector v;
sum = std::accumulate(v.begin(), v.end(),0,
pair_get2nd<my_vector::value_type>());


Michael
 
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