34.绑定器bind1stbind2ndmy_find_if实现

Posted 干锅土鸡

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了34.绑定器bind1stbind2ndmy_find_if实现相关的知识,希望对你有一定的参考价值。

  1. C++ STL中的绑定器
    bind1st:operator()的第一个形参变量绑定成一个确定的值
    bind2nd:operator()的第二个形参变量绑定成一个确定的值
template<typename Container>
void showContainer(Container& con)

	//typename告知编译器Container是个类类型
	typename Container::iterator it = con.begin();
	for(;it!=on.end();++it)
	
		cout << *it << " ";
	
	cout << endl;

int main()

	vector<int> vec;
	srand(time(nullptr));
	for(int i=0;i<20;++i)
	
		vec.push_back(rand()%100+1);
	
	sort(vec.begin(),vec.end());//默认从小到大排序
	sort(vec.begin(),vec.end(),greater<int>());//大到小排序

	//把70按顺序插入到vec容器中:找第一个小于70的数字
	/*
	greater a>b
	less a<b
	绑定器 + 二元函数对象 =》 一元函数对象
	bind1st:+greater bool operator()(70,const _Ty& _Right)
	bind2nd:+less bool operator()(const _Ty& _Left,70)
	*/
	//找第一个小于70的位置
	auto it = find_if(vec.begin(),vec.end(),bind1st(greater<int>(),70));
	//或者:
	//auto it = find_if(vec.begin(),vec.end(),bind2nd(less<int>(),70));
	if(it != vec.end())
	
		vec.insert(it1,70);
	
	return 0;

template<typename Iterator,typename Compare>
Iterator my_find_if(Iterator first,Iterator last,Compare comp)

	for(;first!=last;++first)
	
		if(comp(*first))//重载()
		
			return first;
		
	
	return last;


template<typename Compare,typename T>
class _mybind1st

public:
	_mybind1st(Compare comp,T val)
		:_comp(comp),_val(val)
	
	bool operator()(const T& second)
	
		return _comp(_val,second);
	
private:
	Compare _comp;
	T _val;
:

template<typename Compare,typename T& val>
_mybind1st<Compare,T> mybind1st(Compare comp,const T& val)

	return _mybind1st<Compare,T>(comp,val);

bind2nd和这个实现差不多!

以上是关于34.绑定器bind1stbind2ndmy_find_if实现的主要内容,如果未能解决你的问题,请参考以下文章

.NET Core 自定义模型绑定器调用默认模型绑定器

为properties定制的资源绑定器

资源绑定器

Web API 验证不使用自定义模型绑定器触发

27.泛型算法和绑定器

属性的自定义配置绑定器