Lambda Expression

How would you write the lambda expression for price? Also, how come double v does not have to be initialized?

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
// C21 Records
#include <iostream>
#include <vector>
#include <algorithm>
#include <numeric>

struct Record {
	double unit_price;
	int units;
	//
};

// Why double v does not have to be initialized?
double price(double v, const Record& r) {
	return v + r.unit_price * r.units;
}


int main()
{
	Record r1 = { r1.unit_price = 2.59, r1.units = 500 };
	Record r2 = { r2.unit_price = 4.25, r2.units = 100 };
	Record r3 = { r3.unit_price = 5.75, r2.units = 750 };
	Record r4 = { r4.unit_price = 8.95, r2.units = 150 };

	std::vector<Record> rec;
	rec.push_back(r1);
	rec.push_back(r2);
	rec.push_back(r3);
	rec.push_back(r4);

	for (auto p : rec) {
		std::cout << p.unit_price << '\t' << p.units << '\n';
	}
	double total = accumulate(rec.begin(), rec.end(), 0.0, price);
		std::cout << "total == " << total << '\n';
}
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
#include <iostream>
#include <vector>
#include <algorithm>
#include <numeric>

struct Record {
    double unit_price;
    int units;
    //
};

int main()
{
    std::vector<Record> rec { {2.59, 500}, {4.25,100}, {5.75,750}, {8.98,150} };

    double total = accumulate( rec.begin(), rec.end(), 0.0,
        [] ( double v, const Record& r ) { return v + r.unit_price * r.units; } );
    std::cout << "total == " << total << '\n';

    total = accumulate( rec.begin(), rec.end(), 0.0,
        [] ( auto v, const auto& r ) { return v + r.unit_price * r.units; } ); // auto: C++14
    std::cout << "total == " << total << '\n';

}

http://coliru.stacked-crooked.com/a/b38f98f6ce82b2a3

> Also, how come double v does not have to be initialized?

The accumulated value is initialised with 0.0
double total = accumulate( rec.begin(), rec.end(), 0.0, ... ) ;
For each item, the current accumulated value is passed to the binary function, and updated with the value returned by the function.

See 'Possible implementation: Second version' in: http://en.cppreference.com/w/cpp/algorithm/accumulate
Last edited on
Thanks again JLBorges. Thanks for showing me how to initialize a vector with an ordered pair and how to write the lambda expression.
> initialize a vector with an ordered pair

The same initialisation can be used even if struct Record is not a simple aggregate. For instance:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
struct Record {

    Record( double unit_price, int units, const std::string& descr ) // note: not explicit
        : unit_price(unit_price), units(units), descr(descr) {}
        
    double unit_price;
    int units;
    std::string descr ;
    //
};

int main()
{
    std::vector<Record> rec { {2.59,500,"apples"}, {4.25,100,"nectarines"}, {5.75,750,"uglis"}, {8.98,150,"pears"} };
    // ...
}

http://coliru.stacked-crooked.com/a/c48f3cd125aa4378

The C++11 solution is to allow {}-initializer lists for all initialization
http://www.stroustrup.com/C++11FAQ.html#uniform-init
Topic archived. No new replies allowed.