50 lines
1.3 KiB
C++
50 lines
1.3 KiB
C++
#include <iostream>
|
|
#include <vector>
|
|
#include <algorithm>
|
|
#include <numeric>
|
|
|
|
int main() {
|
|
std::vector<int> a{1,2,3,4};
|
|
std::vector<int> b{5,6,7,8};
|
|
// result vector
|
|
std::vector<double> c(a.size(), 0);
|
|
|
|
// double
|
|
std::for_each(a.begin(), a.end(), [](int &n){ n*=2;});
|
|
|
|
std::cout << "Double:" << std::endl;
|
|
for (auto const &i: a){ std::cout<<i<<" ";}
|
|
std::cout << std::endl;
|
|
|
|
// Reset a
|
|
a = {1,2,3,4};
|
|
|
|
// inverse
|
|
std::transform(a.begin(), a.end(),
|
|
c.begin(),
|
|
[](int i){return 1.0/i;});
|
|
|
|
std::cout << "Multiplicative inverse:" << std::endl;
|
|
for (auto const &i: c){ std::cout << i << " ";}
|
|
std::cout << std::endl;
|
|
|
|
// reziprocal sum
|
|
std::transform(a.begin(), a.end(),
|
|
b.begin(), c.begin(),
|
|
[](int i, int j){return 1.0/(1.0/i + 1.0/j);});
|
|
std::cout << "Reziprocal sum:" << std::endl;
|
|
for (auto const &i: c){ std::cout << i << " ";}
|
|
std::cout << std::endl;
|
|
|
|
// Folds
|
|
int sum = std::accumulate(a.begin(), a.end(), 0);
|
|
int diff = std::accumulate(a.begin(), a.end(), 15, std::minus<int>());
|
|
int mult = std::accumulate(a.begin(), a.end(), 1,
|
|
[](int a, int b)->int { return a * b; });
|
|
std::cout << "Sum: " << sum
|
|
<< "\nDiff from 15: " << diff
|
|
<< "\nProd: " << mult
|
|
<< std::endl;
|
|
|
|
return 0;
|
|
}
|