数值算法
C++ STL的排序算法(Numeric algorithms)是一组对容器元素进行数值计算的模板函数。
一般位于stl_numeric.h文件中。
1、递增赋值itoa
将一组递增的值赋给区间元素。
template<class InIt, class T>
void itoa(ForIt first, ForIt last,T& val);
给迭代器区间[first,last)的元素依次赋值给value,value+1,value+2,...
他是STL的一个扩展算法。
2、元素求和accumulate
将区间元素进行累加求和。
template<class InIt, class T>
T accumulate(InIt first, InIt last, T val);
计算初始值val与迭代器区间[first,last)的各个元素之和(或二元函数对象pr所指的运算)
template<class InIt, class T, class Pred>
T accumulate(InIt first, InIt last, T val, Pred pr);
template<typename _InputIterator, typename _Tp>
_Tp
accumulate(_InputIterator first, _InputIterator last, _Tp init)
{
for ( ; first != last; ++first)
init = init + *first;
return init;
}
#include "numeric"
#include <iostream>
using namespace std;
int multiply(int x,int y){
return x*y;
}
int main(void){
int iArray[5]={1,2,3,4,5};
cout<<accumulate(iArray, iArray+5,0)<<endl;;
return 0;
}
3、两序列元素内积inner_product
对两序列区间的元素进行内积计算。
template<class InIt1, class InIt2, class T>
T product(InIt1 first1, InIt1 last1,
Init2 first2, T val);
计算迭代器区间[first1,last1)和[first2,first2+(last1-first1))元素的内积。或用二元运算pr1和pr2取代内积的加法和乘法,来计算更一般意义的。
template<class InIt1, class InIt2, class T,
class Pred1, class Pred2>
T product(InIt1 first1, InIt1 last1,
Init2 first2, T val, Pred1 pr1, Pred2 pr2);
其实现如下:
template<typename _InputIterator1, typename _InputIterator2, typename _Tp>
_Tp
inner_product(_InputIterator1 first1, _InputIterator1 last1,
_InputIterator2 first2, _Tp init)
{
for ( ; first1 != last1; ++first1, ++first2)
init = init + (*first1 * *first2);
return init;
}
template<typename _InputIterator1, typename _InputIterator2, typename _Tp,typename _BinaryOperation1, typename _BinaryOperation2>
_Tp
inner_product(_InputIterator1 first1, _InputIterator1 last1,
_InputIterator2 first2, _Tp init,
_BinaryOperation1 binary_op1,
_BinaryOperation2 binary_op2)
{
for ( ; first1 != last1; ++first1, ++first2)
init = binary_op1(init, binary_op2(*first1, *first2));
return init;
}
#include <numeric>
#include <iostream>
int add(int x, int y){
return x + y;
}
int mul(int x, int y){
return x * y;
}
int main(void){
using namespace std;
int iArray1[3]={2, 5, 4};
int iArray2[3]={10, 6, 5};
//用原型计算内积
int result=inner_product(iArray1, iArray1 + 3, iArray2, 0);
cout << "数组iArray1与数组iArray2的内积为" << result << endl;
//用原型计算内积
result=inner_product(iArray1, iArray1 +3, iArray2, 0, add, mul);
cout << "数组iArray1与数组iArray2的内积为" << result << endl;
return 0;
}
4、部分元素求和partial_sum
template<class InIt, class OutIt>
OutIt partial_sum(InIt first, InIt last,
OutIt result);
计算部分元素之和(或由pr来指定),计算结果放在以result为起始的地方。即首个元素放在result,头两个元素之和放在result+1,头3个元素之和放在result+2,......
template<class InIt, class OutIt, class Pred>
OutIt partial_sum(InIt first, InIt last,
OutIt result, Pred pr);
#include <numeric>
#include <algorithm>
#include <iostream>
using namespace std;
void print(int x){
cout << x << ' ';
}
int multiply(int x, int y){
return x * y;
}
int main(void){
int iArray[5]={1, 2, 3, 4, 5};
int iResult[5];
//求和
partial_sum(iArray, iArray+5, iResult);
for_each(iResult, iResult+5, print);
cout << endl;
//计算阶乘
partial_sum(iArray, iArray+5, iResult, multiply);
for_each(iResult, iResult+5, print);
cout << endl;
return 0;
}
5、相邻元素求差adjacent_difference
template<class InIt, class OutIt>
OutIt adjacent_difference(InIt first, InIt last,
OutIt result);
计算区间内的差(或由pr指定),结果放在result起始的地方。首个元素放在result处,第一对相邻元素差在result+1处,第二对相邻元素之差放在result+2处,......
template<class InIt, class OutIt, class Pred>
OutIt adjacent_difference(InIt first, InIt last,
OutIt result, Pred pr);
6、n次方计算power。
template<class T, class Integer>
inline T power(T x,Interger n)
template<class T, class Integer,class MoOperation>
inline T power(T x,Interger n, MoOperation op)
是扩展算法,运算时尽量少用乘法(或op运算)。如210=(22)5,45=(42)24,可减少乘法操作次数。
可用于更一般意义的数值算法,如矩阵。