Best/ other way to use lambda in this example

Hi, so I had this exercise where I have to make a lot of implementations for something similar like accumulate(vec.begin, vec.end(), 0) and check for speed afterwards

so I wrote this mixing together lambda and for_each()
1
2
3
4
5
6
7
8
9
double for_each_plus_lambda(const std::vector<double>& vec) {

	double sum{ 0 };
	auto fnc = [&sum](double d) { sum += d; };

	std::for_each(vec.begin(), vec.end(), fnc);

	return sum;
}


I'm wondering if this is the best way to write it if I want to use lambda + for_each().
I mean i looked at for_each() implementation and saw that it actually returns unary function that was passed to it as 3rd argument.
I saw in the example that its easy to create function object and pass it as predicate. Than when passing it as argument I can initialize it with 0 (starting sum) and it keeps track of this sum number until the object is returned and I can check the result.
1
2
3
4
5
6
7
struct pred{
    double sum = 0;
    pred() = default;
    pred(double d):sum{d}{}

    void operator()(double d){sum += d;}
};


Is there anyway I can do stuff like this with lambda if I don't want to use reference to outside variable (sum) to keep track of sum of all values? Or maybe some other way should be preferred?
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include <algorithm>
#include <vector>
#include <iterator>

double sum_v1( const std::vector<double>& vec )
{
    double sum{ 0 };
    const auto fnc = [&sum]( double d ) { sum += d; };

    std::for_each(vec.begin(), vec.end(), fnc);

    return sum;
}

template < typename T > T sum_v2( const std::vector<T>& vec )
{
    T sum{};
    const auto fnc = [&sum]( const T& v ) { sum += v ; };

    std::for_each( vec.begin(), vec.end(), fnc );

    return sum;
}

template < typename CNTR > typename CNTR::value_type sum_v3( const CNTR& seq )
{
    typename CNTR::value_type sum{};
    const auto fnc = [&sum]( const typename CNTR::value_type& v ) { sum += v ; };

    using std::begin ; using std::end ;
    std::for_each( begin(seq), end(seq), fnc );

    return sum;
}

template < typename RANGE > auto sum_v4( const RANGE& seq )
{
    using std::begin ; using std::end ;

    decltype( +*begin(seq) ) sum{};
    const auto fnc = [&sum]( const auto& v ) { sum += v ; };

    std::for_each( begin(seq), end(seq), fnc );

    return sum;
}

template < typename RANGE > auto sum_v5( const RANGE& seq )
{
    using std::begin ; using std::end ;

    decltype( +*begin(seq) ) sum{};

    std::for_each( begin(seq), end(seq), [&sum]( const auto& v ) { sum += v ; } );

    return sum;
}

template < typename RANGE > auto sum_v6( const RANGE& seq )
{
    using std::begin ;
    decltype( +*begin(seq) ) sum{};

    for( const auto& v : seq ) { sum += v ; } ;

    return sum;
}

http://coliru.stacked-crooked.com/a/99a2cd2f91a43cac
Thank you very much for your response and all these examples man! I don't even understand some of the template stuff yet but looks like if I want to use lambda and for_each() I have to use [&sum].

> looks like if I want to use lambda and for_each() I have to use [&sum]

Yes. Unless sum has static storage duration.
1
2
3
4
5
6
7
8
9
10
double sum_v_not_thread_safe( const std::vector<double>& vec )
{
    static double sum{ 0 };
    sum = 0 ;
    const auto fnc = []( double d ) { sum += d; };

    std::for_each(vec.begin(), vec.end(), fnc);

    return sum;
}
TY very much man :)
Topic archived. No new replies allowed.