#include <iostream>
#include <functional>
#include <algorithm>
#include <vector>
#include <iterator>
#include <numeric> //for partial_sum
//算术运算
int main()
{
/************************************************************************/
//plus:二元加
/*
template<class Type>
struct plus : public binary_function <Type, Type, Type>
{
Type operator()(
const Type& _Left,
const Type& _Right
) const;
};
*/
/************************************************************************/
const int n = 5;
std::vector<int> iv1(n);
decltype(iv1) iv2(n);
std::generate(iv1.begin(), iv1.end(), rand);
std::fill(iv2.begin(), iv2.end(), -(RAND_MAX % 100));
//-26 18400 6267 26433 19102
std::transform(iv1.begin(), iv1.end(), iv2.begin(), std::ostream_iterator<int>(std::cout, " "), std::plus<int>());
std::cout << std::endl;
/************************************************************************/
//minus:二元减
/*
template<class Type>
struct minus : public binary_function <Type, Type, Type>
{
Type operator()(
const Type& _Left,
const Type& _Right
) const;
};
*/
/************************************************************************/
//108 18534 6401 26567 19236
std::transform(iv1.begin(), iv1.end(), iv2.begin(), std::ostream_iterator<int>(std::cout, " "), std::minus<int>());
std::cout << std::endl;
/************************************************************************/
//multipies:二元乘
/*
template<class Type>
struct multiplies : public binary_function <Type, Type, Type>
{
Type operator()(
const Type& _Left,
const Type& _Right
) const;
};
*/
/************************************************************************/
iv1.clear();
iv2.clear();
for (auto i : { 2, 3, 4, 5, 6, 7 })
iv1.push_back(i);
std::partial_sum(iv1.begin(), iv1.end(), std::back_inserter(iv2), std::multiplies<int>());
//2 6 24 120 720 5040
std::copy(iv2.begin(), iv2.end(), std::ostream_iterator<int>(std::cout, " "));
std::cout << std::endl;
/************************************************************************/
//divides:二元除
/*
template<class Type>
struct divides : public binary_function <Type, Type, Type>
{
Type operator()(
const Type& _Left,
const Type& _Right
) const;
};
*/
/************************************************************************/
std::vector<double> iv3;
decltype(iv3) iv4;
std::generate_n(std::back_inserter(iv3), 5, rand);
std::transform(iv3.begin(), iv3.end(), std::back_inserter(iv4), std::bind2nd(std::divides<double>(), RAND_MAX));
//0.479873 0.350291 0.895962 0.82284 0.746605
std::copy(iv4.begin(), iv4.end(), std::ostream_iterator<double>(std::cout, " "));
std::cout << std::endl;
/************************************************************************/
//modulus:二元模
/*
template<class Type>
struct modulus : public binary_function <Type, Type, Type>
{
Type operator()(
const Type& _Left,
const Type& _Right
) const;
};
*/
/************************************************************************/
iv1.clear();
std::generate_n(std::back_inserter(iv1), 5, rand);
std::transform(iv1.begin(), iv1.end(), iv1.begin(), std::bind1st(std::modulus<int>(), RAND_MAX));
//4242 4622 9486 15940 2884
std::copy(iv1.begin(), iv1.end(), std::ostream_iterator<int>(std::cout, " "));
std::cout << std::endl;
iv2.clear();
std::generate_n(std::back_inserter(iv2), 5, rand);
std::transform(iv2.begin(), iv2.end(), iv2.begin(), std::bind2nd(std::modulus<int>(), 10));
//1 5 2 7 6
std::copy(iv2.begin(), iv2.end(), std::ostream_iterator<int>(std::cout, " "));
std::cout << std::endl;
/************************************************************************/
//negate:一元取反
/*
template<class Type>
struct negate : public unary_function<Type, Type>
{
Type operator()(
const Type& _Left
) const;
};
*/
/************************************************************************/
//-1 -5 -2 -7 -6
std::transform(iv2.begin(), iv2.end(), std::ostream_iterator<int>(std::cout, " "), std::negate<int>());
std::cout << std::endl;
return 0;
}
====================打个广告,欢迎关注====================
QQ: | 412425870 |
微信公众号:Cay课堂 | |
csdn博客: | http://blog.csdn.net/caychen |
码云: | https://gitee.com/caychen/ |
github: | https://github.com/caychen |
点击群号或者扫描二维码即可加入QQ群: | |
|