Detailed explanation of numerical algorithms of STL standard library (detailed explanation of accumulate, inner_product, partial_sum, adjacent_difference functions)

A numerical algorithm is a numerical computation performed on the contents of a container

STL's numerical algorithm implements four types of calculations, namely addition, subtraction, multiplication, and division. These calculations can be performed on a sequence of values. The numerical algorithm is shown in the table:

function illustrate
accumulate(first,last,init) The calculation result is the sum of all elements plus the value of init, and the return value is a number type
inner_product(fist,last,first2,init) The calculation result is to multiply and sum the corresponding elements, and the summed value plus init is the final result
partial_sum(first,last,result) The calculation result is that the nth element is the sum of the first n items (including itself), and result is a container for saving the final result
adjacent_difference(first,last,result) The calculation result is that the nth element is the result of subtracting the n-1 item from the nth item, and result is the container for saving the result

The code is implemented as follows, and there are detailed explanations of the four functions in the code:

#include<iostream>
#include<vector>
#include<algorithm>
#include<numeric> 	//需要加入数值算法的头文件 
using namespace std;
void output(int val)
{
	cout << " " << val;
}
int multi(int a,int b)
{
	return a*b;
}
int main()
{
	vector<int> ivc1,ivc2;
	int n,x;
	cout << "请输入元素个数:" << endl;
	cin >> n;
	cout << "请输入各个元素的值:" << endl;
	for(int i = 0; i < n; i++)
	{
		cin >> x;
		ivc1.push_back(x);
	}
	for(int i = 0; i < n; i++)
	{
		cin>>x;
		ivc2.push_back(x);
	}
	cout << "ivc1:" << endl;
	for_each(ivc1.begin(),ivc1.end(),output);
	cout << endl;
	cout << "ivc2:" << endl;
	for_each(ivc2.begin(),ivc2.end(),output);
	cout << endl << endl;
	
	/*
		今天详细介绍一下数值算法嘿嘿
		数值算法是对容器中的内容进行数值上的计算
		STL的数值算法实现了4种类型的计算,可以在一个值序列上进行这些计算。
	*/ 
	/*
		using accumulate(first,last,init)
		first是指 指向第一个元素的迭代器,last是指 指向最后一个元素的迭代器,init是一个数值,与容器之中的元素求和 
		accumulate 算法是用来对该容器进行累加求和的算法,返回一个最终累加的结果
	*/ 
	int result = accumulate(ivc1.begin(),ivc1.end(),7);
	cout << "After accumulate():" << endl;
		cout << result<<endl<<endl;
	
	/*
		inner_product(first,last,first2,init)
		first,last,first2都是指容器中的开始结尾的迭代器
		而init是在两个容器对应元素相乘后加上的元素
		具体代码如下: 
	*/
	cout << "After inner_product():" << endl;
	int inner1 = inner_product(ivc1.begin(),ivc1.end(),ivc2.begin(),0);
	cout << "init = 0:" << endl;
	cout << inner1 << endl; 
	int inner2 = inner_product(ivc1.begin(),ivc1.end(),ivc2.begin(),2);
	cout << "init = 2:" << endl;
	cout << inner2 << endl;
	cout << endl;
	
	/*
		partial_sum(first,last,result)
		first,last都是指向容器的一个迭代器 
		1.容器要计算的起始位置,容器要计算的结束位置,结果存放的起始位置 
		2.容器要计算的起始位置,容器要计算的结束位置,结果存放的起始位置,自定义函数
		局部求和,第一个元素是本身,第二个元素是1.2元素之和,第三个元素是1.2.3元素之和,以此类推。 
	*/ 
	
	cout << "After partial_sum():" << endl;
	partial_sum(ivc1.begin(),ivc1.end(),ivc2.begin());
	cout << "Use partial deal with ivc1:" << endl;
	for_each(ivc2.begin(),ivc2.end(),output);
	cout << endl;
	partial_sum(ivc1.begin(),ivc1.end(),ivc2.begin(),multi);
	cout << "Use partial(multi) deal with ivc1:" << endl;
	for_each(ivc2.begin(),ivc2.end(),output);
	cout << endl << endl;
	
	/*
		adjacent_difference(first,last,result)
		(容器所要计算的起始位置,容器所要计算的结束位置,计算结果的存储的起始位置) 
		第一个元素是本身,第二个元素是2-1的结果,第三个元素是3-2的结果,以此类推 
	*/	
	cout << "After adjacent_difference():" << endl;
	adjacent_difference(ivc1.begin(),ivc1.end(),ivc2.begin());
	for_each(ivc2.begin(),ivc2.end(),output);
	cout << endl;
}

The result of the operation is shown in the figure below:

watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5bCP5L6v5LiN6Lq65bmzLg==,size_14,color_FFFFFF,t_70,g_se,x_16

For a detailed explanation, please refer to the text description between the code segments. You can try to modify the code yourself to realize the algorithm you want to complete. Doing it by hand will be much better than relying on your brain. 

 

 

Guess you like

Origin blog.csdn.net/m0_61886762/article/details/124224837