{
return value >= minRange && value <= maxRange;
}
private:
const int minRange, maxRange;
}
std::count_if(
v.begin(), v.end(),
Labmda_1(minRange, maxRange) ()
);
, -. , - , - . :
#include <map>
#include <string>
#include <iostream>
#include <algorithm>
int main ()
{
typedef std::map< std::string, double > Prices;
Prices prices;
prices[ "Apple" ] = 9.50;
prices[ "Orange" ] = 8.30;
prices[ "Banana" ] = 2.20;
double maximumPriceLimit;
std::cout << "Enter price limit: ";
std::cin >> maximumPriceLimit;
std::for_each(
prices.begin(), prices.end(),
[ maximumPriceLimit ] (const Prices::value_type & v)
{
if (v.second > maximumPriceLimit)
std::cout << "Item " << v.first << " is too expensive" << std::endl;
}
);
}
, 5.00:
- , . - . , , . .
, - , - , - . std::accumulate -, . , - :
#include <map>
#include <string>
#include <iostream>
#include <algorithm>
#include <numeric>
int main ()
{
typedef std::map< std::string, double > Prices;
Prices prices;
prices[ "Apple" ] = 9.50;
prices[ "Orange" ] = 8.30;
prices[ "Banana" ] = 2.20;
typedef std::map< std::string, double > Order;
Order myOrder;
myOrder[ "Apple" ] = 2.5;
myOrder[ "Orange" ] = 1.3;
myOrder[ "Banana" ] = 0.6;
double orderTotalCost =
std::accumulate(
myOrder.begin(), myOrder.end(),
0.0,
[ & prices ] (double total, Order::value_type vt)
{
return total + vt.second * prices[ vt.first ];
}
);
std::cout << "Order totally takes: " << orderTotalCost << std::endl;
|
|
}
- , . , std::for_each. - - :
double orderTotalCost = 0.0;
std::for_each(
myOrder.begin(), myOrder.end(),
[ & ] (const Order::value_type & vt)
{
orderTotalCost += vt.second * prices[ vt.first ];
}
);
, - , = &..
, , - . , . - , - , .
double orderTotalCost = 0.0;
std::for_each(
myOrder.begin(), myOrder.end(),
[ &, maxPriceLimit ] (const Order::value_type & vt)
{
double price = prices[ vt.first ];
if (price > maxPriceLimit)
orderTotalCost += vt.second * prices[ vt.first ];
}
);
, - . , , - , .., . , .
, , 2016. ( , , ). , - . (18%), , , (1.5%). , - , . , - - (, 22%, , ).
, - 4000. 720 .. (18%) 60 (1.5%). -, 4000-720-60, .. 3220 . , - 22% 4000, 880 . , - 3220 . 1660 . , 51.5% -.
- . , - - + .
|
|
#include <unordered_map>
#include <algorithm>
#include <iostream>
#include <string>
#include <functional>
int main ()
{
typedef std::unordered_map< std::string, double > Salaries;
Salaries salaries;
salaries[ "Ivanov" ] = 1000.0;
salaries[ "Petrov" ] = 1200.0;
salaries[ "Sidorov" ] = 800.0;
const double personalIncomeTax = 0.18;
const double salarySocialTax = 0.22;
const double militaryTax = 0.015;
double totalSalaryWithTaxes = 0.0;
std::for_each(
salaries.cbegin(), salaries.cend(),
[=, & totalSalaryWithTaxes ] (const Salaries::value_type & v)
{
double netto = v.second;
double personalBrutto = netto /
(1.0 - personalIncomeTax - militaryTax);
double totalBrutto = personalBrutto * (1 + salarySocialTax);
totalSalaryWithTaxes += totalBrutto;
}
);
std::cout << "Total salary + taxes to pay: " << totalSalaryWithTaxes << std::endl;
}
https://github.com/zaychenko-sergei/oop-samples/tree/master/lec8
.
- , , . . - , , -, . , , , , std::bind, . std::function, , , .
. -. , .
, -, .
.