#include <iostream>
#include <algorithm>
#include <numeric> //for inner_product, accumulate, partial_sum
#include <iterator>
#include <vector>
#include <time.h> //for time
//数值算法
int main()
{
/************************************************************************/
//accumulate
/************************************************************************/
//accumulate:
/*
template<class InputIterator, class Type>
Type accumulate(
InputIterator _First,
InputIterator _Last,
Type _Val
);
template<class InputIterator, class Type, class BinaryOperation>
Type accumulate(
InputIterator _First,
InputIterator _Last,
Type _Val,
BinaryOperation _Binary_op
);
*/
srand((unsigned)time(NULL));
std::vector<int> iv;
generate_n(back_inserter(iv), 10, []{return rand() % 10;});
int totalSum = std::accumulate(iv.begin(), iv.end(), 0);
std::cout << totalSum << std::endl;
std::vector<int> iv2;
//对iv进行排序并拷贝到iv2中
std::sort(iv.begin(),iv.end());
std::unique_copy(iv.begin(), iv.end(), std::back_inserter(iv2));
std::copy(iv2.begin(), iv2.end(), std::ostream_iterator<int>(std::cout, " "));
std::cout << std::endl;
//将0替换成1
std::replace(iv2.begin(), iv2.end(), 0, 1);
std::copy(iv2.begin(), iv2.end(), std::ostream_iterator<int>(std::cout, " "));
std::cout << std::endl;
//计算乘积
totalSum = std::accumulate(iv2.begin(), iv2.end(), 1, std::multiplies<int>());
std::cout << totalSum << std::endl;
/************************************************************************/
//inner_product
/************************************************************************/
//inner_product:计算[first1,last1)和[first2,first2+(last2-last1))进行一般化内积
//注意:版本1使用简单的\
: _Val + (a1 * b1) + ... + (ai * bi)
// 版本2使用\
: _Val _Binary_op1 (a1 _Binary_op2 b1) _Binary_op1 (a2 _Binary_op2 b2) _Binary_op1 ... _Binary_op1 (ai _Binary_op2 bi)
/*
template<class InputIterator1, class InputIterator2, class Type>
Type inner_product(
InputIterator1 _First1,
InputIterator1 _Last1,
InputIterator2 _First2,
Type _Val
);
template<class InputIterator1, class InputIterator2, class Type,
class BinaryOperation1, class BinaryOperation2>
Type inner_product(
InputIterator1 _First1,
InputIterator1 _Last1,
InputIterator2 _First2,
Type _Val,
BinaryOperation1 _Binary_op1,
BinaryOperation2 _Binary_op2
);
*/
int A1[] = {1, 2, 3};
int A2[] = {4, 1, -2};
const int N = sizeof(A1) / sizeof(A1[0]);
// 0 + 1*4 + 2*1 + 3*-2
std::cout << std::inner_product(A1, A1 + N, A2, 0) << std::endl; //0
std::vector<int> iv3 = {3,5,7,2};
std::vector<int> iv4 = {1,8,2,-9,9};
// 0 + 3*1 + 5*8 + 7*2 + 2*-9
std::cout << std::inner_product(iv3.begin(), iv3.end(), iv4.begin(), 0) << std::endl; //39
iv3.clear();
iv4.clear();
iv3 = {1,2,3};
iv4 = {3,2,1};
//1 * (1+3) * (2+2) * (3+1) = 64
std::cout << std::inner_product(iv3.begin(), iv3.end(), iv4.begin(), 1,
std::multiplies<int>(), std::plus<int>()) << std::endl; //64
/************************************************************************/
//partial_sum
/************************************************************************/
//partial_sum:计算部分和,
//将*first赋值给*result,将*first和*(first+1)的运算结果赋值给*(result+1),将*first与*(first+1)与*(first+2)的运算结果赋值给*(result+2),依次类推
//即:把*first赋值给*result,把*(first+1)和*result的运算结果赋值给*(result+1),把*(first+2)与*(result+1)的运算结果赋值给*(result+2),以此类推
//版本1:使用简单的operator+运算
//版本2:使用外部的function object对象进行运算
/*
template<class InputIterator, class OutIt>
OutputIterator partial_sum(
InputIterator _First,
InputIterator _Last,
OutputIterator _Result
);
template<class InputIterator, class OutIt, class Fn2>
OutputIterator partial_sum(
InputIterator _First,
InputIterator _Last,
OutputIterator _Result,
BinaryOperation _Binary_op
);
*/
//测试版本1:
//iv3 = {1,2,3}
// 1 3 6
std::partial_sum(iv3.begin(), iv3.end(), std::ostream_iterator<int>(std::cout, " "));
std::cout << std::endl;
//测试版本2:
iv4.clear();
iv4 = { 2, 8, 5, 9 };
//2 16 80 720
std::partial_sum(iv4.begin(), iv4.end(), std::ostream_iterator<int>(std::cout, " "),
std::multiplies<int>());
std::cout << std::endl;
/************************************************************************/
//adjacent_difference
/************************************************************************/
//adjacent_difference:计算[first,last)相邻元素的差额,与partial_sum互为相反运算
//版本1:使用默认的operator-来运算
//版本2:使用外部function object对象运算
/*
template<class InputIterator, class OutIterator>
OutputIterator adjacent_difference(
InputIterator _First,
InputIterator _Last,
OutputIterator _Result
);
template<class InputIterator, class OutIterator, class BinaryOperation>
OutputIterator adjacent_difference(
InputIterator _First,
InputIterator _Last,
OutputIterator _Result,
BinaryOperation _Binary_op
);
*/
//测试版本1:
std::vector<int> iv5(10);
int i = 0;
for_each(iv5.begin(), iv5.end(),[&](int& value){i++; value = i * i; });
//1 4 9 16 25 36 49 64 81 100
std::copy(iv5.begin(), iv5.end(), std::ostream_iterator<int>(std::cout, " "));
std::cout << std::endl;
std::vector<int> iv6;
std::adjacent_difference(iv5.begin(), iv5.end(), std::back_inserter(iv6));
//1 3 5 7 9 11 13 15 17 19
std::copy(iv6.begin(), iv6.end(), std::ostream_iterator<int>(std::cout, " "));
std::cout << std::endl;
//1 4 9 16 25 36 49 64 81 100
std::partial_sum(iv6.begin(), iv6.end(), std::ostream_iterator<int>(std::cout, " "));
std::cout << std::endl;
//测试版本2:使用plus<int>()来代替默认的operator-运算
//1 4 8 12 16 20 24 28 32 36
std::adjacent_difference(iv6.begin(), iv6.end(), std::ostream_iterator<int>(std::cout, " "),
std::plus<int>());
std::cout << std::endl;
return 0;
}
====================打个广告,欢迎关注====================
QQ: | 412425870 |
csdn博客: | http://blog.csdn.net/caychen |
码云: | https://gitee.com/caychen/ |
github: | https://github.com/caychen |
点击群号或者扫描二维码即可加入QQ群: | |