C ++中的transform_inclusive_scan()函数
在本教程中,我们将讨论一个程序,以了解C++中的transform_inclusive_scan()函数。
示例
#include <iostream>
#include <vector>
using namespace std;
namespace point_input_iterator {
template <class InputItrator, class OutputItrator, class BinaryOperation, class UnaryOperation>
OutputItrator transform_inclusive_scan(InputItrator first,
InputItrator last,
OutputItrator d_first,
BinaryOperation binary_op,
UnaryOperation unary_op){
*d_first = unary_op(*first);
first++;
d_first++;
for (auto it = first; it != last; it++) {
//计算前缀和
*d_first = binary_op(unary_op(*it), *(d_first - 1));
d_first++;
}
return d_first;
}
}
int main(){
//使用vector输入元素
vector<int> InputVector{ 11, 22, 33, 44, 55, 66, 77, 88 };
vector<int> OutputVector(8);
point_input_iterator::transform_inclusive_scan(InputVector.begin(), InputVector.end(), OutputVector.begin(), [](auto xx, auto yy) {
return xx + yy;
},
[](auto xx) {
return xx * xx;
});
for (auto item : OutputVector) {
//打印输出项
cout << item << " ";
}
cout << std::endl;
return 0;
}输出结果
121 605 1694 3630 6655 11011 16940 24684
热门推荐
10 八一幼儿祝福语大全简短
11 公司乔迁食堂祝福语简短
12 婚礼结束聚餐祝福语简短
13 儿媳买车妈妈祝福语简短
14 毕业送礼老师祝福语简短
15 同事辞职正常祝福语简短
16 恭贺新婚文案祝福语简短
17 金店立秋祝福语简短英文
18 婆婆高寿祝福语大全简短