c++ 高阶函数 numeric库

#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;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值