-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprint_stats.cpp
More file actions
71 lines (63 loc) · 2.39 KB
/
Copy pathprint_stats.cpp
File metadata and controls
71 lines (63 loc) · 2.39 KB
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
68
69
70
71
#include <vector>
#include <algorithm>
#include <iostream>
using namespace std;
// enum class Gender
// {
// FEMALE,
// MALE
// };
// struct Person
// {
// int age; // возраст
// Gender gender; // пол
// bool is_employed; // имеет ли работу
// };
// // Это пример функции, его не нужно отправлять вместе с функцией PrintStats
// template <typename InputIt>
// int ComputeMedianAge(InputIt range_begin, InputIt range_end)
// {
// if (range_begin == range_end)
// {
// return 0;
// }
// vector<typename InputIt::value_type> range_copy(range_begin, range_end);
// auto middle = begin(range_copy) + range_copy.size() / 2;
// nth_element(
// begin(range_copy), middle, end(range_copy),
// [](const Person & lhs, const Person & rhs)
// {
// return lhs.age < rhs.age;
// }
// );
// return middle->age;
// }
void PrintStats(vector<Person> persons)
{
cout << "Median age = " << ComputeMedianAge(persons.begin(), persons.end()) << endl;
auto gender_it = partition(persons.begin(), persons.end(), [](const Person& p) { return p.gender == Gender::FEMALE; });
cout << "Median age for females = " << ComputeMedianAge(persons.begin(), gender_it) << endl;
cout << "Median age for males = " << ComputeMedianAge(gender_it, persons.end()) << endl;
auto job_it = partition(persons.begin(), gender_it, [](const Person& p) { return p.is_employed; });
cout << "Median age for employed females = " << ComputeMedianAge(persons.begin(), job_it) << endl;
cout << "Median age for unemployed females = " << ComputeMedianAge(job_it, gender_it) << endl;
job_it = partition(gender_it, persons.end(), [](const Person& p) { return p.is_employed; });
cout << "Median age for employed males = " << ComputeMedianAge(gender_it, job_it) << endl;
cout << "Median age for unemployed males = " << ComputeMedianAge(job_it, persons.end()) << endl;
}
// int main()
// {
// vector<Person> persons =
// {
// {31, Gender::MALE, false},
// {40, Gender::FEMALE, true},
// {24, Gender::MALE, true},
// {20, Gender::FEMALE, true},
// {80, Gender::FEMALE, false},
// {78, Gender::MALE, false},
// {10, Gender::FEMALE, false},
// {55, Gender::MALE, true},
// };
// PrintStats(persons);
// return 0;
// }