# C++初学者指南-5.标准库(第二部分)--数值运算算法

57 篇文章 1 订阅
52 篇文章 2 订阅

## C++初学者指南-5.标准库(第二部分)–数值运算算法

#include <numeric>
Reductions 根据输入元素的序列产生一个结果。
Scans 生成一个结果序列，其元素数量与输入序列相同。

### iota (注意不是itoa函数)

std::vector<int> v;
v.resize(9,0);
// fill subrange (as shown in image)
iota(begin(v)+2, begin(v)+7, 1);
for (int x : v) { cout << x << ' '; }  // 0 0 1 2 3 4 5 0 0
// fill entire vector
iota(begin(v), end(v), 3);
for (int x : v) { cout << x << ' '; }  // 3 4 5 6 7 8 9 10 11

std::vector<int> v;
v.resize(5,0);
// NOTE: might not be available yet
// in many standard library implementations!
auto const result = ranges::iota(v, 3);
std::cout << result.value;  // 8
for (int x : v) { cout << x << ' '; }  // 3 4 5 6 7


### Reductions

#### reduce

cppreference

std::vector<int> v {1,9,7,3,2,8};
auto const sum = reduce(begin(v), end(v));  // 1+9+7+3+2+8 = 30
auto const s47 = reduce(begin(v), end(v), 47);  // 47+1+9+7+3+2+8 = 77
std::vector<double> w {2.0, 1.5, 3.0, 1.5};
auto const product = reduce(begin(w), end(w), 1.0, std::multiplies<>{});
// double product = 1.0 * 2.0 * 1.5 * 3.0 * 1.5 = 13.5
// 并行执行: #include <execution>
auto const psum = reduce(std::execution::par, begin(v), end(v));


// 较窄的初始值类型可能会导致信息丢失:
std::vector<double> v {1.2, 2.4};
auto const wtf = reduce(begin(v), end(v), 1);
cout << wtf;  // 4                    int ^
auto const sum = reduce(begin(v), end(v), 1.0);
cout << sum;  // 4.6               double ^^^


#### transform_reduce

cppreference

std::vector<int> v {3,2,4};
auto f = [](int x) { return x*x; };
auto const rf = transform_reduce(begin(v), end(v), 1, std::plus<>{}, f);
// rf = 1 + f(3) + f(2) + f(4) = 30


cppreference

std::vector<double> x {1.0, 3.0, 5.0};
std::vector<double> y {2.0, 4.0, 8.0};
auto const rx1 = transform_reduce(begin(x), end(x), begin(y), 10.0);
// rx1 = 10 + (1⋅2)+(3⋅4)+(5⋅8) = 64
auto const rx2 = transform_reduce(
begin(x), end(x), begin(y), 0.0,
std::plus<>{}, std::divides<>{});
// rx2 = 0.0 + (1/2)+(3/4)+(5/8) = 1.875


#### 遗留操作（无法并行执行）

##### accumulate (≈ reduce) C++98

cppreference

std::vector<int> v {1,9,7,3,2,8};
auto const sum = accumulate(begin(v), end(v), 0);  // 1+9+7+3+2+8 = 30
auto const s47 = accumulate(begin(v), end(v), 47);  // 47+1+9+7+3+2+8 = 77
std::vector<double> w {2.0, 1.5, 3.0, 1.5};
auto const product = accumulate(begin(w), end(w), 1.0, std::multiplies<>{});
// double product = 1.0 * 2.0 * 1.5 * 3.0 * 1.5 = 13.5


// 较窄的初始值类型可能会导致信息丢失:
std::vector<double> v {1.2, 2.4};
auto const wtf = accumulate(begin(v), end(v), 0);
cout << wtf;  // 3                  int ^
auto const sum = accumulate(begin(v), end(v), 0.0);
cout << sum;  // 3.6             double ^^^


##### inner_product (≈ transform_reduce) C++98

cppreference

std::vector<int> v {4,3,2,1};
std::vector<int> w {10,20,30,40};
auto const ip = inner_product(begin(v), end(v), begin(w), 50);
// ip = 50 + (4⋅10)+(3⋅20)+(2⋅30)+(1⋅40) = 250
std::vector<double> num {1.0, 3.0, 5.0};
std::vector<double> den {2.0, 4.0, 8.0};
auto const res = inner_product(
begin(num), end(num), begin(den), 0.0,
std::plus<>{}, std::divides<>{} );
// res = 0.0 + (1/2)+(3/4)+(5/8) = 1.875


### Scans (Prefix ‘Sums’)

cpprefernece

std::vector<int> in {1,2,6,8,3,6};
// 确认输出能存放计算结果
std::vector<int> out;
out.resize(in.size());
for (int x : out) { cout << x << ' '; }  // 1 1 4 2 -5 3
// C++17: 能提供定制操作
for (int x : out) { cout << x << ' '; }  // 1 3 8 14 11 9
for (int x : out) { cout << x << ' '; }  // 1 2 12 48 24 18


#### inclusive_scan

cppreference

std::vector<int> in {2,1,7,5,3};
// make sure output can fit results
std::vector<int> out;
out.resize(in.size());
inclusive_scan(begin(in), end(in), begin(out));
for (int x : out) { cout << x << ' '; }  // 2 3 10 15 18
inclusive_scan(begin(in), end(in), begin(out), std::minus<>{});
for (int x : out) { cout << x << ' '; }  // 2 1 -6 -11 -14
// with offset '3':
inclusive_scan(begin(in), end(in), begin(out), std::plus<>{}, 3);
for (int x : out) { cout << x << ' '; }  // 5 6 13 18 21


#### exclusive_scan

cppreference

std::vector<int> in {2,1,7,5,3};
// make sure output can fit results
std::vector<int> out;
out.resize(in.size());
exclusive_scan(begin(in), end(in), begin(out), 0);
for (int x : out) { cout << x << ' '; }  // 0 2 3 10 15
// with offset '3':
exclusive_scan(begin(in), end(in), begin(out), 3);
for (int x : out) { cout << x << ' '; }  // 3 5 6 13 18
exclusive_scan(begin(in), end(in), begin(out), 0, std::minus<>{});
for (int x : out) { cout << x << ' '; }  // 0 -2 -3 -10 -15


#### transform_inclusive_scan

cppreference

// returns value only if even and 0 if odd
auto even_only = [](int x) { return (x & 1) ? 0 : x; };
std::vector<int> in {2,1,6,4,3};
// make sure output can fit results
std::vector<int> out;
out.resize(in.size());
transform_inclusive_scan(
begin(in), end(in), begin(out), std::plus<>{}, even_only);
for (int x : out) { cout << x << ' '; }  // 2 2 8 12 12
// with offset '3':
transform_inclusive_scan(
begin(in), end(in), begin(out), std::plus<>{}, even_only, 3);
for (int x : out) { cout << x << ' '; }  // 5 5 11 15 15
// with projection f(x) = -x2:
transform_inclusive_scan(
begin(in), end(in), begin(out), std::plus<>{},
[](int x) { return -(x*x); });
for (int x : out) { cout << x << ' '; }  // -4 -5 -41 -57 -66


#### transform_exclusive_scan

cppreference

// returns value only if even and 0 if odd
auto even_only = [](int x) { return (x & 1) ? 0 : x; };
std::vector<int> in {2,1,6,4,3};
// make sure output can fit results
std::vector<int> out;
out.resize(in.size());
transform_exclusive_scan(
begin(in), end(in), begin(out), 0, std::plus<>{}, even_only);
for (int x : out) { cout << x << ' '; }  // 0 2 2 8 12
// with offset '3':
transform_exclusive_scan(
begin(in), end(in), begin(out), 3, std::plus<>{}, even_only);
for (int x : out) { cout << x << ' '; }  // 3 5 5 11 15
// with projection f(x) = -x2:
transform_exclusive_scan(
begin(in), end(in), begin(out), 0, std::plus<>{},
[](int x) { return -(x*x); });
for (int x : out) { cout << x << ' '; }  // 0 -4 -5 -41 -57


#### 遗留操作（无法并行执行）

##### partial_sum (≈ inclusive_scan) C++98

cppreference

std::vector<int> in {1,1,2,2,1,1};
// make sure output can fit results
std::vector<int> out;
out.resize(in.size());
partial_sum(begin(in), end(in), begin(out));
for (int x : out) { cout << x << ' '; }  // 1 2 4 6 7 8
partial_sum(begin(in), end(in), begin(out), std::minus<>{});
for (int x : out) { cout << x << ' '; }  // 1 0 -2 -4 -5 -6


### 相关内容

03-03 380
07-14 750
09-13 689
09-09 784
09-08 1051
09-10 824
09-08 1023
09-08 438

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