#include <numeric>
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
//*************************************** 求值高阶函数
// 1. accumulate reduce 是对整个求值
// 类似于 (init op a1) op a2 op a3 op a4
// 2. inner_product, 是两个集合求值, 生成一个值
// 是 init + (a1 * b1) + (a2 * b2) + (a3 * b3)
// 指定操作函数 init op1 (a1 op2 b1) op1 (a2 op2 b2)
// 3. transform_reduce,
// 比 reduce 多了一个操作针对每个元素的 transform
// 它的两个集合的版本和 inner_product 很像
// 类似于 init op1 (op2(a1)) op1 (op2(a2))
// 两个集合的版本 init op1 (a1 op2 b1) op1 (a2 op2 b2)
// ************************************** 生成集合
// 4. partial_sum
// 类似于 t = a1, b1 = t
// x = t op a2, t = x, b2 = t;
// x = t op a3, t = x, b3 = t;
// 5. exclusive_scan, plus -> transform_exclusive_scan
//类似 t = init, x = x op a1, b1 = t
// t = x, x = x op a2, b2 = t
// 6. inclusive_scan, plus -> transform_inclusive_scan
//类似 init = init op a1, b1 = init;
// init = init op a2, b2 = init;
// 7. adjacent_difference
// b1 = a1
// b2 = a2 op a1
// b3 = a3 op a2
// 8. iota
// iota(_ForwardIterator __first, _ForwardIterator __last, _Tp __value_)
// {
// for (; __first != __last; ++__first, (void) ++__value_)
// *__first = __value_;
// }
// 9. gcd, lcm
int main() {
vector<int> data = {1, 2, 3, 4, 5};
vector<int> result;
transform(data.begin(), data.end(), back_inserter(result), [](int v) {
return v * v + 1;
});
for_each(result.begin(), result.end(), [](int v) {
cout << v << "\t";
});
cout << endl;
partial_sum(data.begin(), data.end(), result.begin());
for_each(result.begin(), result.end(), [](int v) {
cout << v << "\t";
});
cout << endl;
exclusive_scan(data.begin(), data.end(), result.begin(), 0);
for_each(result.begin(), result.end(), [](int v) {
cout << v << "\t";
});
cout << endl;
inclusive_scan(data.begin(), data.end(), result.begin());
for_each(result.begin(), result.end(), [](int v) {
cout << v << "\t";
});
cout << endl;
}
c++ 高阶函数 numeric库
最新推荐文章于 2024-07-24 14:05:37 发布