, .
, , , :
#include <string>
#include <vector>
#include <functional>
#include <algorithm>
#include <iostream>
// ,
class Employee
{
public:
Employee (const std::string & _name, int _age, double _salary)
: m_name(_name),
m_age(_age),
m_salary(_salary)
{}
const std::string & getName () const { return m_name; }
int getAge () const { return m_age; }
double getSalary () const { return m_salary; }
private:
std::string m_name;
int m_age;
double m_salary;
};
//
void printEmployees (const std::vector< Employee * > & _employees)
{
for (auto it = _employees.begin(); it!= _employees.end(); ++it)
std::cout << (*it)->getName()
<< " age=" << (*it)->getAge()
<< " salary=" << (*it)->getSalary()
<< std::endl;
}
int main ()
{
std::vector< Employee * > employees;
employees.push_back(new Employee("Ivanov", 25, 1000.0));
employees.push_back(new Employee("Ivanov", 45, 2000.0));
employees.push_back(new Employee("Petrov", 50, 750.0));
employees.push_back(new Employee("Sidorov", 34, 1200.0));
//...
}
, , , , . std::sort, <. , - - , , , .
, . :
std::sort(
employees.begin(), employees.end(),
std::bind(
std::greater< int >(),
std::bind(& Employee::getAge, std::placeholders::_1),
std::bind(& Employee::getAge, std::placeholders::_2)
)
);
std::cout << "Sorted by age descenting: " << std::endl;
printEmployees(employees);
std::cout << std::endl;
, std::greater std::less:
std::sort(
employees.begin(), employees.end(),
std::bind(
std::less< int >(),
|
|
std::bind(& Employee::getAge, std::placeholders::_1),
std::bind(& Employee::getAge, std::placeholders::_2)
)
);
std::cout << "Sorted by age ascenting: " << std::endl;
printEmployees(employees);
std::cout << std::endl;
:
std::sort(
employees.begin(), employees.end(),
std::bind(
std::greater< double >(),
std::bind(& Employee::getSalary, std::placeholders::_1),
std::bind(& Employee::getSalary, std::placeholders::_2)
)
);
std::cout << "Sorted by salary descenting: " << std::endl;
printEmployees(employees);
std::cout << std::endl;
( ), - , - . true, , - . , :
std::sort(
employees.begin(), employees.end(),
std::bind(
std::logical_or< bool >(),
std::bind< bool >(
std::less< std::string >(),
std::bind(& Employee::getName, std::placeholders::_1),
std::bind(& Employee::getName, std::placeholders::_2)
),
std::bind< bool >(
std::logical_and< bool >(),
std::bind< bool >(
std::equal_to< std::string >(),
std::bind(& Employee::getName, std::placeholders::_1),
std::bind(& Employee::getName, std::placeholders::_2)
),
std::bind< bool >(
std::less< int >(),
std::bind(& Employee::getAge, std::placeholders::_1),
std::bind(& Employee::getAge, std::placeholders::_2)
)
)
)
);
std::cout << "Sorted by name ascenting, then by age ascenting: " << std::endl;
printEmployees(employees);
std::cout << std::endl;
:
1 , , . .
-
++11 , , , . ++11 , . - (lambda expressions), .
, , , :
|
|
-:
std::transform(
vX.begin(), vX.end(), vY.begin(),
std::back_inserter(vR),
[] (double x, double y)
{
return 3.0 * x / (y + 2.0);
}
);
, , .
- . , . , double. , return, . :
std::transform(
vX.begin(), vX.end(), vY.begin(),
std::back_inserter(vR),
[] (double x, double y) -> double
{
return 3.0 * x / (y + 2.0);
}
);
-? , - . -. , - . - . . .., , , .
:
struct Lambda_1
: public std::binary_function< double, double, double >
// ^ ^ ^
// .1 .2
{