新网创想网站建设,新征程启航

为企业提供网站建设、域名注册、服务器等服务

C++STLbind1stbind2ndbind的使用方法

本篇内容主要讲解“C++ STL bind1st bind2nd bind 的使用方法”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“C++ STL bind1st bind2nd bind 的使用方法”吧!

创新互联建站"三网合一"的企业建站思路。企业可建设拥有电脑版、微信版、手机版的企业网站。实现跨屏营销,产品发布一步更新,电脑网络+移动网络一网打尽,满足企业的营销需求!创新互联建站具备承接各种类型的成都网站制作、网站建设项目的能力。经过10余年的努力的开拓,为不同行业的企事业单位提供了优质的服务,并获得了客户的一致好评。

说明

bind1st() 和 bind2nd(),在 C++11 里已经 deprecated 了,建议使用新标准的 bind()
下面先说明bind1st() 和 bind2nd()的用法,然后在说明bind()的用法。

头文件

#include

作用

bind1st()bind2nd()都是把二元函数转化为一元函数,方法是绑定其中一个参数。
bind1st()是绑定第一个参数。
bind2nd()是绑定第二个参数。

例子

#include #include  #include  using namespace std; int main() { int numbers[] = { 10,20,30,40,50,10 }; int cx; cx = count_if(numbers, numbers + 6, bind2nd(less(), 40)); cout << "There are " << cx << " elements that are less than 40.\n"; cx = count_if(numbers, numbers + 6, bind1st(less(), 40)); cout << "There are " << cx << " elements that are not less than 40.\n"; system("pause"); return 0; }
There are 4 elements that are less than 40.There are 1 elements that are not less than 40.

分析
less()是一个二元函数,less(a, b)表示判断a是否成立。

所以bind2nd(less(), 40)相当于x<40是否成立,用于判定那些小于40的元素。

bind1st(less(), 40)相当于40是否成立,用于判定那些大于40的元素。

bind()

bind1st() 和 bind2nd(),在 C++11 里已经 deprecated 了.bind()可以替代他们,且用法更灵活更方便。

上面的例子可以写成下面的形式:

#include #include  #include  using namespace std; int main() { int numbers[] = { 10,20,30,40,50,10 }; int cx; cx = count_if(numbers, numbers + 6, bind(less(), std::placeholders::_1, 40)); cout << "There are " << cx << " elements that are less than 40.\n"; cx = count_if(numbers, numbers + 6, bind(less(), 40, std::placeholders::_1)); cout << "There are " << cx << " elements that are not less than 40.\n"; system("pause"); return 0; }

std::placeholders::_1 是占位符,标定这个是要传入的参数。
所以bind()不仅可以用于二元函数,还可以用于多元函数,可以绑定多元函数中的多个参数,不想绑定的参数使用占位符表示。
此用法更灵活,更直观,更便捷。

到此,相信大家对“C++ STL bind1st bind2nd bind 的使用方法”有了更深的了解,不妨来实际操作一番吧!这里是创新互联网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!


分享标题:C++STLbind1stbind2ndbind的使用方法
链接URL:http://www.wjwzjz.com/article/isjpgi.html