accumulate instead of for-loop

Discussion in 'C++' started by Alex Vinokur, Apr 3, 2008.

  1. Alex Vinokur

    Alex Vinokur Guest

    Alex Vinokur, Apr 3, 2008
    #1
    1. Advertising

  2. Alex Vinokur

    Boris Glawe Guest

    1. Advertising

  3. Alex Vinokur

    James Kanze Guest

    Re: accumulate instead of for-loop

    On Apr 3, 1:24 pm, Alex Vinokur <> 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:
    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
    James Kanze, Apr 3, 2008
    #3
  4. Alex Vinokur

    Jerry Coffin Guest

    In article <21e0cb00-9ae3-4f87-805c-13c6679fe9a3
    @i36g2000prf.googlegroups.com>, 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::pair<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.
    Jerry Coffin, Apr 3, 2008
    #4
  5. 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
    Michael DOUBEZ, Apr 3, 2008
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Brian K. Michalk

    64 bit add and accumulate with MMX

    Brian K. Michalk, Sep 20, 2003, in forum: Perl
    Replies:
    6
    Views:
    646
    Terje Mathisen
    Sep 20, 2003
  2. Roedy Green

    Accumulate 1.0

    Roedy Green, Jul 17, 2004, in forum: Java
    Replies:
    0
    Views:
    379
    Roedy Green
    Jul 17, 2004
  3. accumulate() usage

    , Feb 2, 2006, in forum: C++
    Replies:
    1
    Views:
    348
    Neelesh Bodas
    Feb 2, 2006
  4. Skirmish
    Replies:
    9
    Views:
    975
    James Kanze
    Jul 24, 2007
  5. Isaac Won
    Replies:
    9
    Views:
    364
    Ulrich Eckhardt
    Mar 4, 2013
Loading...

Share This Page