C++ STL(27):大小比较

29 篇文章 0 订阅
29 篇文章 1 订阅
#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群:

328243383(1群)




点击群号或者扫描二维码即可加入QQ群:

180479701(2群)





  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值