STL 源码分析 # stl_number #

STL 源码分析 # stl_number #


/***********************************************************
Programmer	:	EOF
e-mail		:	[email protected]
Date		:	2015.04.04
File		:	6number.cpp

************************************************************/
#include <numeric>
#include <vector>
#include <functional>
#include <iostream>
#include <iterator>

using namespace std;

int main(int argc, char const *argv[])
{
	int ia[5] = {1,2,3,4,5};
	vector<int> iv(ia, ia + 5);

	cout << accumulate(iv.begin(), iv.end(), 0) << endl;

	cout << accumulate(iv.begin(), iv.end(), 0, minus<int>() ) << endl;

	cout << inner_product(iv.begin(), iv.end(), iv.begin(), 10) << endl;

	cout << inner_product(iv.begin(), iv.end(), 
						iv.begin(), 10, minus<int>(), plus<int>()) 
	<< endl;

	ostream_iterator<int> oite(cout, " ");

	partial_sum(iv.begin(), iv.end(), oite);

	partial_sum(iv.begin(), iv.end(), oite, minus<int>());

	adjacent_difference(iv.begin(), iv.end(), oite);

	adjacent_difference(iv.begin(), iv.end(), oite, plus<int>());


	for(int i = 0; i < iv.size(); i++)
	{
		cout << iv.at(i) << " ";
	}
	cout << endl;

	return 0;
}


STL 源码分析 # stl_number #_第1张图片


accumulate()的实现

STL 源码分析 # stl_number #_第2张图片


内积 inner_product()

/STL 源码分析 # stl_number #_第3张图片



partial_sum()和之前的accumulate()不一样,partial_sum()可能会把一步步积分的结果粗村在__result指向的
"连续数据结构内".之所以这么说,是因为,这货可能是数组,也可能是其他支持++运算符迭代器的类型.



把相邻的数据做差分,然后储存在__result指向的数据结构内

STL 源码分析 # stl_number #_第4张图片



power()的实现.不过话说我主机里面的STL 还没有支持power...不能用.

STL 源码分析 # stl_number #_第5张图片





STL 源码分析 # stl_number #_第6张图片

你可能感兴趣的:(C++)