Thread: std::for_each over a vector of vector

  1. #1
    Registered User
    Join Date
    Jan 2007
    Posts
    330

    std::for_each over a vector of vector

    How to accumulate a two dimensional array with std::for_each?

    Code:
    std::vector<std::vector<long> >  v;
    
    std::for_each(v.begin(), v.end(), ????);

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    When you say accumulate, are you thinking of std::accumulate instead of std::for_each()?

    Anyway, it is quite simple: just make the function or function object able to handle a std::vector<long> as the argument.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jan 2007
    Posts
    330
    You mean something like this?

    Code:
    class Acc
    {
    public:
    	Acc() : m_total(0) {}
    
    	void operator()(std::vector<long> &v)
    	{
    		m_total += std::accumulate(v.begin(), v.end(), 0);
    	}
    
    private:
    	long m_total;
    };
    I dont see the advantage anymore of just doing it the "old fashioned" way then with a loop. Was hoping for a better solution

  4. #4
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    No.

    You need to use `std::accumulate' correctly and ignore `std::for_each'.

    In other words, use a tool that was designed to solve the problem you want to solve.

    Edit: Not that you can't do it with `std::for_each'; you just need to use one or the other.

    Soma
    Last edited by phantomotap; 05-11-2009 at 04:35 AM.

  5. #5
    Registered User
    Join Date
    Jan 2007
    Posts
    330
    Im just asking if there is a one-liner to accumulate all items in a vector of vector of long. Doesnt matter if I use accumulate or for_each or whatever

    Code:
    vector<vector<long> > v;
    
    total = sum(v.begin(), v.end(), 0);
    ??

  6. #6
    The larch
    Join Date
    May 2006
    Posts
    3,573
    If you are so keen on doing it as a one-liner, you might read on nesting STL algorithms with Boost.Lambda.

    Perhaps it just ain't worth it...

    -----

    With a struct it might look like this (a bit simpler than yours, and the struct could technically be a free function):

    Code:
    #include <vector>
    #include <numeric>
    #include <iostream>
    
    using namespace std;
    
    struct subaccumulator { 
        long operator()(long r, const vector<long>& v) const
        {
            return r + accumulate(v.begin(), v.end(), 0l); 
        }
    };
        
    int main()
    {
        vector<vector<long> > v(2, vector<long>(3, 4)); //2 by 3 all 4's    
        cout << accumulate( v.begin(), v.end(), 0l, subaccumulator()) << '\n';       
    }
    Last edited by anon; 05-11-2009 at 05:36 AM.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  7. #7
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    "Im just asking if there is a one-liner to accumulate all items in a vector of vector of long."

    Yes. Infinitely many...

    Of course, that doesn't mean that you will not have to code some support routines yourself, but then that's the beauty of C++ generics. You will only have to code it once!

    Soma

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. can some one please tell me the cause of the error ?
    By broli86 in forum C Programming
    Replies: 8
    Last Post: 06-26-2008, 08:36 PM
  2. syntax help?
    By scoobygoo in forum C++ Programming
    Replies: 1
    Last Post: 08-07-2007, 10:38 AM
  3. Vector class
    By Desolation in forum C++ Programming
    Replies: 2
    Last Post: 05-12-2007, 05:44 PM
  4. Need some help/advise for Public/Private classes
    By nirali35 in forum C++ Programming
    Replies: 8
    Last Post: 09-23-2006, 12:34 PM
  5. Certain functions
    By Lurker in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2003, 01:26 AM