-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathdrill21-3.cpp
44 lines (35 loc) · 1.29 KB
/
drill21-3.cpp
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
#include <algorithm>
#include <iostream>
#include <iterator>
#include <numeric>
#include <vector>
using namespace std;
int main() {
istream_iterator<double> iit(cin), eos;
vector<double> vd(iit, eos);
ostream_iterator<double> oit(cout, "\n");
cout << "vd:\n";
copy(vd.begin(), vd.end(), oit);
vector<int> vi(vd.size());
copy(vd.begin(), vd.end(), vi.begin());
cout << "\n(vd[i], vi[i]) pairs:\n";
for (int i = 0; i < vd.size(); ++i)
cout << '(' << vd[i] << ',' << vi[i] << ')' << endl;
double sum_vd = accumulate(vd.begin(), vd.end(), 0.0);
cout << "\nsum(vd) = " << sum_vd << endl;
cout << "sum(vd) - sum(vi) = " << (sum_vd - accumulate(vi.begin(), vi.end(), 0)) << endl;
reverse(vd.begin(), vd.end());
cout << "\nreversed vd:\n";
copy(vd.begin(), vd.end(), oit);
double mean_vd = sum_vd / vd.size();
cout << "\naverage(vd) = " << mean_vd << endl;
vector<double> vd2(vd.size());
auto it = copy_if(vd.begin(), vd.end(), vd2.begin(), [mean_vd](double d) { return d < mean_vd; });
vd2.erase(it, vd2.end());
cout << "\nelements of vd with values less than the mean:\n";
copy(vd2.begin(), vd2.end(), oit);
sort(vd.begin(), vd.end());
cout << "\nsorted vd:\n";
copy(vd.begin(), vd.end(), oit);
return 0;
}