#include <iostream>
#include <functional>
#include <algorithm>
#include <iterator>
#include <vector>
//大小比较
int main()
{
/************************************************************************/
//equal_to:等值比较
/*
template<class Type>
struct equal_to : public binary_function<Type, Type, bool>
{
bool operator()(
const Type& _Left,
const Type& _Right
) const;
};
*/
/************************************************************************/
int A[] = { 1, 3, 0, 2, 5, 9, 0, 0, 6, 0 };
std::stable_partition(std::begin(A), std::end(A), std::bind2nd(std::equal_to<int>(), 0));
//等价std::stable_partition(std::begin(A), std::end(A),[](const int& each){return each == 0; });
//0 0 0 0 1 3 2 5 9 6
std::copy(std::begin(A), std::end(A), std::ostream_iterator<int>(std::cout, " "));
std::cout << std::endl;
/************************************************************************/
//not_equal_to:不等值
/*
template<class Type>
struct not_equal_to : public binary_function<Type, Type, bool>
{
bool operator()(
const Type& _Left,
const Type& _Right
) const;
};
*/
/************************************************************************/
int *pNonZero = std::find_if(std::begin(A), std::end(A), std::bind2nd(std::not_equal_to<int>(), 0));
//等价int *pNonZero = std::find_if(std::begin(A), std::end(A), [](const int& each){return each != 0; });
//The first nonzero is 1 in index: 4
std::cout << "The first nonzero is " << *pNonZero << " in index: " << pNonZero - A << std::endl;
/************************************************************************/
//less:小于
/*
template<class Type>
struct less : public binary_function <Type, Type, bool>
{
bool operator()(
const Type& _Left,
const Type& _Right
) const;
};
*/
/************************************************************************/
int B[] = { 1, 3, 6, 9, 5, 8, 4, 2, 7 };
int index = 0;
std::for_each(std::begin(B), std::end(B), [&index](int& each)
{
each = (index % 2 != 0 ? -each : each);
index++;
});
std::stable_partition(std::begin(B), std::end(B), std::bind2nd(std::less<int>(), 0));
//等价std::stable_partition(std::begin(B), std::end(B), [](const int& each){return each < 0; });
//-3 -9 -8 -2 1 6 5 4 7
std::copy(std::begin(B), std::end(B), std::ostream_iterator<int>(std::cout, " "));
std::cout << std::endl;
/************************************************************************/
//greater:大于
/*
template<class Type>
struct greater : public binary_function <Type, Type, bool>
{
bool operator()(
const Type& _Left,
const Type& _Right
) const;
};
*/
/************************************************************************/
std::sort(std::begin(B), std::end(B), std::greater<int>());
//7 6 5 4 1 -2 -3 -8 -9
std::copy(std::begin(B), std::end(B), std::ostream_iterator<int>(std::cout, " "));
std::cout << std::endl;
/************************************************************************/
//less_equal:小于等于
/*
template<class Type>
struct less_equal : public binary_function <Type, Type, bool>
{
bool operator()(
const Type& _Left,
const Type& _Right
) const;
};
*/
/************************************************************************/
std::vector<int> iv;
std::remove_copy_if(std::begin(B), std::end(B), std::back_inserter(iv), std::bind2nd(std::less_equal<int>(), 3));
//7 6 5 4
std::copy(iv.begin(), iv.end(), std::ostream_iterator<int>(std::cout, " "));
std::cout << std::endl;
/************************************************************************/
//greater_equal:大于等于
/*
template<class Type>
struct greater_equal : public binary_function <Type, Type, bool>
{
bool operator()(
const Type& _Left,
const Type& _Right
) const;
};
*/
/************************************************************************/
std::sort(std::begin(B), std::end(B));
//-9 -8 -3 -2 1 4 5 6 7
std::copy(std::begin(B), std::end(B), std::ostream_iterator<int>(std::cout, " "));
std::cout << std::endl;
int *pFirstGe4 = std::find_if(std::begin(B), std::end(B), std::bind2nd(std::greater_equal<int>(), 4));
//The first greater equal 4 is 4 in index : 5
std::cout << "The first greater equal 4 is " << *pFirstGe4 << " in index: " << pFirstGe4 - B << std::endl;
return 0;
}
====================打个广告,欢迎关注====================
QQ: | 412425870 |
微信公众号:Cay课堂 | ![]() |
csdn博客: | http://blog.csdn.net/caychen |
码云: | https://gitee.com/caychen/ |
github: | https://github.com/caychen |
点击群号或者扫描二维码即可加入QQ群: | ![]() |
| ![]() |