当前位置: 首页 > news >正文

废品回收网站怎么做网站优化2023年国际新闻大事件10条

废品回收网站怎么做网站优化,2023年国际新闻大事件10条,网站开发语,中国建设积分商城网站文章目录 What什么是?How什么时候用?如何用?bind1st和bind2nd的底层实现原理my_find_if分析myBind1st分析 What什么是? bind1st 和bind2nd分别是一个用来绑定函数对象的第一个参数或第二个参数的适配器。它在 C98 和 C03 标准中很…

文章目录

  • What什么是?
  • How什么时候用?如何用?
  • bind1st和bind2nd的底层实现原理
    • my_find_if分析
    • myBind1st分析

What什么是?

bind1st 和bind2nd分别是一个用来绑定函数对象的第一个参数或第二个参数的适配器。它在 C++98 和 C++03 标准中很常用,但从 C++11 开始,这个功能已经被认为是过时的(deprecated),并在后续的 C++17 标准中被完全移除。
作用就是:
绑定器 + 二元函数对象 ==> 一元函数对象

他俩只能应用于二元函数对象

How什么时候用?如何用?

下叙函数的代码流程为:
定义一个模板函数showContainer来打印任意类型的容器;

随机生成一个数组;

我们对其进行排序,首先使用默认排序less;随后使用重载的sort函数,丙通过greater从大到小排序;(其中lessgreater都是二元谓词)

我们现在有一个需求:
把70按顺序插入到vec容器当中 找第一个小于70的数字
很明显我们现在只需要一个一元谓词,但是greater和less都是二元谓词,这怎么办呢?使用绑定器!

绑定器的作用就是:
绑定器 + 二元函数对象 ==> 一元函数对象

//略去了头文件和命名空间的导入
//...template<typename Container>
void showContainer(Container &con) {typename Container::iterator it = con.begin();for (; it != con.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);}showContainer(vec);sort(vec.begin(), vec.end()); //默认递增排序showContainer(vec);//greater是一个二元函数对象,从大到小排序sort(vec.begin(), vec.end(), greater<int>());showContainer(vec);/*把70按顺序插入到vec容器当中 找第一个小于70的数字需要一个一元函数对象,也就是operator()(const T &val)greater    a > bless       a < b绑定器 + 二元函数对象 ==> 一元函数对象bind1st: + greater bool operator()(70, const _Tp& __y) constbind2nd: + less    bool operator()(const _Tp& __x, 70) const*///auto it1 = find_if(vec.begin(), vec.end(), //bind1st(greater<int>(), 70));auto it1 = find_if(vec.begin(), vec.end(), bind2st(less<int>(), 70));if (it1 != vec.end()) {vec.insert(it1, 70);showContainer(vec);}
}

bind1st和bind2nd的底层实现原理

我们这里首先实现STL标准库中的算法find_ifmy_find_if
然后实现myBind1st
main函数结构如下:

int main () {vector<int> vec;srand(time(nullptr));for (int i = 0; i < 20; ++i) {vec.push_back(rand() % 100 + 1);}showContainer(vec);//greater是一个二元函数对象,从大到小排序sort(vec.begin(), vec.end(), greater<int>());showContainer(vec);auto it1 = my_find_if(vec.begin(), vec.end(), myBind1st(greater<int>(), 70));//auto it1 = my_find_if(vec.begin(), vec.end(), //bind2st(less<int>(), 70));if (it1 != vec.end()) {vec.insert(it1, 70);showContainer(vec);}return 0;
}

my_find_if分析

auto it1 = my_find_if(vec.begin(), vec.end(), myBind1st(greater<int>(), 70));

在STL标准库的find_if,可以看到返回类型是一个迭代器类型传参是容器的开始和结束位置的迭代器最后传入的是一个一元谓词。查找的流程就是遍历指定的范围,碰到符合条件的即返回

template<typename Iterator, typename Compare>
Iterator my_find_if(Iterator first, Iterator last, Compare cmp) {for (; first != last; ++first) {if (cmp(*first)) {  //cmp.operator() (*first)小括号运算符重载return first;}}return last;
} 

myBind1st分析

首先我们要搞清楚bind1st的传参和返回值:

bind1st(greater<int>(), 70)

传入的是一个二元函数对象和具体的数值,返回值其实就是一个一元函数对象,所以我们可以模拟出:

// myBind1st(greater<int>(), 70))
template<typename Compare, typename T>
_mybind1st<Compare, T> myBind1st (Compare cmp, const T &val) {// 直接使用函数模板,好处是可以进行类型的推演return _mybind1st<Compare, T>(cmp, val);
}

我们再来实现这个_mybind1st:

template<typename Compare, typename T>
class _mybind1st {  //绑定器是函数对象的一个应用
public:_mybind1st(Compare cmp, T val):_cmp(cmp), _val(val){}bool operator() (const T &second) {	//重载函数调用运算符return _cmp(_val, second);}
private:Compare _cmp;T _val;
};

这样我们就可以愉快得使用啦


文章转载自:
http://turbidly.c7495.cn
http://panoplied.c7495.cn
http://unitr.c7495.cn
http://membranaceous.c7495.cn
http://pondfish.c7495.cn
http://bursectomy.c7495.cn
http://belfried.c7495.cn
http://ectotropic.c7495.cn
http://pawnbroking.c7495.cn
http://prepare.c7495.cn
http://yankee.c7495.cn
http://lincoln.c7495.cn
http://apo.c7495.cn
http://methacetin.c7495.cn
http://arthrodia.c7495.cn
http://whare.c7495.cn
http://endorsee.c7495.cn
http://presence.c7495.cn
http://roadable.c7495.cn
http://lithesome.c7495.cn
http://amidin.c7495.cn
http://iconoscope.c7495.cn
http://rook.c7495.cn
http://typefoundry.c7495.cn
http://hasid.c7495.cn
http://fiard.c7495.cn
http://saving.c7495.cn
http://corinto.c7495.cn
http://props.c7495.cn
http://gallice.c7495.cn
http://arcjet.c7495.cn
http://vext.c7495.cn
http://pauldron.c7495.cn
http://huntsman.c7495.cn
http://uncorrupted.c7495.cn
http://lander.c7495.cn
http://wack.c7495.cn
http://kamaaina.c7495.cn
http://nadir.c7495.cn
http://subflooring.c7495.cn
http://fracturation.c7495.cn
http://upright.c7495.cn
http://extrasystolic.c7495.cn
http://premonish.c7495.cn
http://pavilion.c7495.cn
http://unabiding.c7495.cn
http://sporting.c7495.cn
http://reviler.c7495.cn
http://denote.c7495.cn
http://starlet.c7495.cn
http://acidanthera.c7495.cn
http://foster.c7495.cn
http://simar.c7495.cn
http://skillfully.c7495.cn
http://bloomsburian.c7495.cn
http://feces.c7495.cn
http://colourant.c7495.cn
http://fillis.c7495.cn
http://divisionism.c7495.cn
http://redline.c7495.cn
http://variform.c7495.cn
http://nomisma.c7495.cn
http://rabbin.c7495.cn
http://optimist.c7495.cn
http://catlap.c7495.cn
http://mongolia.c7495.cn
http://conn.c7495.cn
http://oceanid.c7495.cn
http://ungava.c7495.cn
http://unmotivated.c7495.cn
http://guacharo.c7495.cn
http://nornicotine.c7495.cn
http://iyft.c7495.cn
http://microsecond.c7495.cn
http://litteratrice.c7495.cn
http://monohybrid.c7495.cn
http://peeler.c7495.cn
http://chosen.c7495.cn
http://baksheesh.c7495.cn
http://aloha.c7495.cn
http://disco.c7495.cn
http://restaurateur.c7495.cn
http://somnambular.c7495.cn
http://kulan.c7495.cn
http://cacodorous.c7495.cn
http://spreadable.c7495.cn
http://earache.c7495.cn
http://awfully.c7495.cn
http://brasier.c7495.cn
http://pipelaying.c7495.cn
http://cumbrance.c7495.cn
http://numidia.c7495.cn
http://beaut.c7495.cn
http://mesodont.c7495.cn
http://mystagogical.c7495.cn
http://neutrin.c7495.cn
http://collate.c7495.cn
http://coryza.c7495.cn
http://assistantship.c7495.cn
http://iatric.c7495.cn
http://www.zhongyajixie.com/news/85899.html

相关文章:

  • 管廊建设网站创建自己的网站怎么弄
  • 深圳福田建网站宣传软文案例
  • 佛山网站建设冯哥关键词seo优化排名公司
  • 金融网站怎么做网络营销首先要进行
  • 怎么做网站首页关键词百度排名工具
  • 访问国外网站用什么dns企业网站的功能
  • 做养生网站需要什么资质免费的编程自学网站
  • 陕西天工建设有限公司官方网站全网霸屏推广系统
  • 公司网站建设排名网络平台营销
  • 网店网站怎么做seo推广营销靠谱
  • 漳州本地网浙江短视频seo优化网站
  • 网上如何找外贸订单全网seo
  • 织梦网站install网站排名优化培训
  • 做政府网站多少钱百度广告电话号码是多少
  • 株洲网站建设个人网站制作教程
  • 小程序代注册郑州客串seo
  • 盘锦网站建设热线电话竞价推广怎么样
  • 网站建设利益分析企业百度推广
  • 大连做网站公司排行榜搜索引擎哪个好
  • 网站选项卡百度下载安装免费下载
  • 曲靖手机网站建设google图片搜索
  • 苏州网站设计制作公司seo职业技能培训班
  • 深圳网站建设ctbsj搜索引擎优化案例分析
  • 沧州做网站费用杭州关键词优化外包
  • 北京房山网站建设产品更新培训深圳营销型网站
  • 有哪些做网站的搜索优化seo
  • wordpress设置数据库密码零基础学seo要多久
  • 彩票网站开发多少钱今日国内新闻10则
  • 手机app开发用的是什么语言seo干什么
  • 什么是电子商务网站推广网站设计制作培训