STL学习笔记数值算法
STL学习笔记--数值算法
数值算法
C++ STL 的数值算法(Numeric algorithms)是一组对容器元素进行数值计算的模板函数,包括容器元素求和 accumulate 、两序列元素的内积 inner_product 、容器元素的一系列部分元素和 partial_sum 、容器每对相邻元素的差。
应用 accumulate 算法求数组元素和、元素积
1 /* 下面示例程序对数组 iArray={1, 2, 3, 4, 5}进行元素求和、求积
2 */
3 ----------------------------------------------- 应用 accumulate 算法求数组元素和、元素积
4 #include <numeric>
5 #include <iostream>
6 using namespace std;
7
8 int multiply( int x, int y)
9 {
10 return x * y;
11 }
12
13 int main()
14 {
15 int iArray[ 5 ] = { 1 , 2 , 3 , 4 , 5 };
16 cout << " 数组 iArray 的元素和为: "
17 << accumulate(iArray, iArray+ 5 , 0 )
18 << endl << endl;
19
20 cout << " 数组 iArray 的元素积为: "
21 << accumulate(iArray, iArray+ 5 , 1 , multiply)
22 << endl;
23
24 return 0 ;
25 }
应用 inner_product 算法计算两数组内积
1 /* 下面示例程序分别使用 inner_product 算法的两种用法,计算两数组{2, 5, 4} 和{10, 6, 5}的内积
2 */
3 ----------------------------------------------- 应用 inner_product 算法计算两数组内积
4 #include <numeric>
5 #include <iostream>
6 using namespace std;
7
8 int add( int x, int y)
9 {
10 return x + y;
11 }
12
13 int mul( int x, int y)
14 {
15 return x * y;
16 }
17
18 int main()
19 {
20 int iArray1[ 3 ] = { 2 , 5 , 4 };
21 int iArray2[ 3 ] = { 10 , 6 , 5 };
22
23 // 第1中计算方法
24 int result = inner_product(iArray1, iArray1+ 3 , iArray2, 0 );
25 cout << " 数组iArray1与数组iArray2的内积为: " << result << endl << endl;
26
27 // 第2中计算方法(扩展性更强)
28 result = inner_product(iArray1, iArray1+ 3 , iArray2, 0 , add, mul);
29 cout << " 数组iArray1与数组iArray2的内积为: " << result << endl << endl;
30
31 return 0 ;
32 }
应用 partial_sum 算法计算数组的一系列部分元素和
1 /* 下面示例程序先对数组 iArray={1, 2, 3, 4, 5}进行局部求和,打印输出为"1 3 6 10 15",然后选择 binary_op 为乘法运算,打印出各个局部阶乘积为“1 2 6 24 120”
2 */
3 ----------------------------------------------- 应用 partial_sum 算法计算数组的一系列部分元素和
4 #include <numeric>
5 #include <algorithm>
6 #include <iostream>
7 using namespace std;
8
9 void print( int x)
10 {
11 cout << x << " " ;
12 }
13
14 int multiply( int x, int y)
15 {
16 return x * y;
17 }
18
19 int main()
20 {
21 int iArray[ 5 ] = { 1 , 2 , 3 , 4 , 5 };
22 int iResult[ 5 ];
23
24 // 求和
25 partial_sum(iArray, iArray+ 5 , iResult);
26 for_each(iResult, iResult+ 5 , print);
27 cout << endl << endl;
28
29 // 计算阶乘
30 partial_sum(iArray, iArray+ 5 , iResult, multiply);
31 for_each(iResult, iResult+ 5 , print);
32 cout << endl;
33
34 return 0 ;
35 }
应用adjacent_difference 算法计算数组的一系列临近元素差的积
1 /* 下面示例程序,先对数组 iArray={1, 10, 17, 25, 30}的各个邻近元素求差,打印输出"1 9 7 8 5",然后用乘法运算代替求差,计算各个相邻元素的乘积,打印输出为“1 10 170 425 750”
2 */
3 ----------------------------------------------- 应用adjacent_difference 算法计算数组的一系列临近元素差的积
4 #include <numeric>
5 #include <algorithm>
6 #include <iostream>
7 using namespace std;
8
9 void print( int x)
10 {
11 cout << x << " " ;
12 }
13
14 int multiply( int x , int y)
15 {
16 return x * y;
17 }
18
19 int main()
20 {
21 int iArray[ 5 ] = { 1 , 10 , 17 , 25 , 30 };
22 int iResult[ 5 ];
23 // 邻近元素差
24 adjacent_difference(iArray, iArray+ 5 , iResult);
25 for_each(iResult, iResult+ 5 , print);
26 cout << endl << endl;
27
28 // 邻近元素相乘
29 adjacent_difference(iArray, iArray+ 5 , iResult, multiply);
30 for_each(iResult, iResult+ 5 , print);
31 cout << endl;
32
33 return 0 ;
34 }
----------------------------------- 数值算法小结
介绍了C++ STL 针对容器的元素进行数值计算的算法,虽然简单且为数不多,但是计算形式具有相当的灵活性。
例如:
容器元素求和的 accumulate 算法,可设置运算为乘法操作,就可变成对容器元素的阶乘计算;
两序列容器的元素内积 inner_product 算法,可推广到对应元素两两执行一种运算后,再将结果执行另一种另一种运算;
容器元素的部分元素和 parital_sum 算法,可设置相应的操作运算,变成一系列部分元素的阶乘计算;
相邻元素差 adjacent_difference 算法,可转换为计算相邻元素的和或者乘积 等等。
作者: Music__Liang
出处: http://HdhCmsTestcnblogs测试数据/music-liang/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利.
分类: STL学习笔记
标签: STL , C++ , 算法
作者: Leo_wl
出处: http://HdhCmsTestcnblogs测试数据/Leo_wl/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
版权信息