日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

STL之函数对象

發布時間:2024/4/11 编程问答 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 STL之函数对象 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

為了使類屬算法具有靈活性,STL常使用函數的重載機制為算法提供兩種形式。算法的第一種形式使用的是常規的操作來實現。第二種形式中,算法可以根據用戶指定的準測對元素經行處理。

函數對象包含了一個可以通過函數調用運算符()使用的函數。實際上,函數對象是重載了函數調用運算符operator()的類模板。

用戶可以創建自己的函數對象。STL提供了算術函數對象,關系函數對象,邏輯函數對象。


算術函數對象:

  • plus<type> ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 加
  • minus<type> ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?減
  • multiplies<type> ? ? ? ? ? ? ? ? ? ? ? 乘
  • divides<type> ? ? ? ? ? ? ? ? ? ? ? ? ? ?除
  • modulus<type> ? ? ? ? ? ? ? ? ? ? ? ?模
  • negate<type> ? ? ? ? ? ? ? ? ? ? ? ? ? ?取反
  • 示例代碼:

  • #include?<iostream>??
  • #include?<algorithm>??
  • #include?<string>??
  • #include?<numeric>??
  • #include?<iterator>??
  • #include?<vector>??
  • #include?<functional>??
  • ??
  • using?namespace?std;??
  • ??
  • int?functAdd(plus<int>,int,int);??
  • ??
  • int?main()?{??
  • ????//?加法函數對象??
  • ????plus<int>?addNum;??
  • ????int?sum?=?addNum(34,56);??
  • ????cout?<<?"Sum="?<<?sum?<<?endl;??
  • ??
  • ????//?字符串拼接??
  • ????plus<string>?joinString;??
  • ????string?s1?=?"hello";??
  • ????string?s2?=?"There";??
  • ??
  • ????string?str?=?joinString(s1,s2);??
  • ????cout?<<?"str="?<<?str?<<?endl;??
  • ??
  • ????//?調用外部函數??
  • ????cout?<<?"funcAdd="?<<?functAdd(addNum,34,26)?<<?endl;??
  • ??
  • ????int?list[8]?=?{1,2,3,4,5,6,7,8};??
  • ????vector<int>?intsList(list,list+8);??
  • ????ostream_iterator<int>?screenOut(cout,?"?");??
  • ??
  • ????cout?<<?"intList:";??
  • ????copy(intsList.begin(),intsList.end(),screenOut);??
  • ????cout?<<?endl;??
  • ??
  • ????//累計??
  • ????int?suma?=?accumulate(intsList.begin(),intsList.end(),0);??
  • ????cout?<<?"accumulate:"?<<?suma?<<?endl;??
  • ??
  • ????int?product?=?accumulate(intsList.begin(),intsList.end(),1,multiplies<int>());??
  • ??
  • ????cout?<<?"product:"?<<?product?<<?endl;??
  • ??
  • ????return?0;??
  • }??
  • ??
  • int?functAdd(plus<int>?sum,int?x,int?y)?{??
  • ????return?sum(x,?y);??
  • }??

  • 運行結果:

    intQueue.front:26
    intQueue.back:33
    intQueue.front:18
    intQueue :
    18 50 33


    關系函數對象:

  • equal_to<type> ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?等于
  • not_equal_to<type> ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?不等于
  • greater<type> ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 大于
  • greater_equal<type> ? ? ? ? ? ? ? ? ? ? ? ? ? ? 大于等于
  • less<type> ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?小于
  • less_equal<type> ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 小于等于
  • 示例代碼:

  • #include?<iostream>??
  • #include?<algorithm>??
  • #include?<string>??
  • #include?<numeric>??
  • #include?<iterator>??
  • #include?<vector>??
  • #include?<functional>??
  • ??
  • using?namespace?std;??
  • ??
  • int?main()?{??
  • ????//?等于??
  • ????equal_to<int>?compare;??
  • ????bool?isEqual?=?compare(6,6);??
  • ????cout?<<?"isEqual="?<<?isEqual?<<?endl;??
  • ??
  • ????//?大于??
  • ????greater<string>?greaterString;??
  • ????string?s1?=?"hello";??
  • ????string?s2?=?"there";??
  • ??
  • ????if?(greaterString(s1,?s2))??
  • ????{??
  • ????????cout?<<?s1?<<?"?is?greater?than?"?<<?s2?<<?endl;??
  • ????}?else?{??
  • ????????cout?<<?s2?<<?"?is?greater?than?"?<<?s1?<<?endl;??
  • ????}??
  • ??
  • ????int?temp[8]?=?{2,3,4,5,1,7,8,9};??
  • ????vector<int>?vecList(temp,temp+8);??
  • ????vector<int>::iterator?intItr1,intItr2;??
  • ????ostream_iterator<int>?screen(cout,?"?");??
  • ????cout?<<?"vecList:"?<<endl;??
  • ????copy(vecList.begin(),vecList.end(),screen);??
  • ????cout?<<?endl;??
  • ??
  • ????intItr1?=?adjacent_find(vecList.begin(),vecList.end(),greater<int>());??
  • ????intItr2?=?intItr1?+?1;??
  • ??
  • ????cout?<<?"intItr1:"?<<?*intItr1?<<",intItr2:"?<<?*intItr2?<<?endl;??
  • ????cout?<<?"psition:"?<<?vecList.end()?-?intItr2?<<?endl;??
  • ????cout?<<?"psition:"?<<?intItr2?-?vecList.begin()?<<?endl;??
  • ??
  • ????return?0;??
  • }??

  • 運行結果:

    isEqual=1
    there is greater than hello
    vecList:
    2 3 4 5 1 7 8 9
    intItr1:5,intItr2:1
    psition:4
    psition:4


    邏輯運算對象:

  • logical_not<type>
  • logical_and<type>
  • logical_or<type>
  • 總結

    以上是生活随笔為你收集整理的STL之函数对象的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。

    主站蜘蛛池模板: 五十路息子 | 大地资源影视在线播放观看高清视频 | aa片在线观看视频在线播放 | 亚洲三级av | 日韩福利在线视频 | 黄色免费网站在线观看 | 在线观看色网站 | 欧美日韩不卡一区二区 | 亚洲午夜在线观看 | av日韩av| 看日本黄色录像 | 玉米地疯狂的吸允她的奶视频 | www.黄色片网站 | 在线色亚洲 | 三级不卡| 碰在线视频 | 一本色道久久综合亚洲精品小说 | 久久综合网址 | 国产又粗又黄又爽的视频 | 欧美裸体视频 | 国产精品--色哟哟 | 日韩av在线网址 | 中文字幕精品一区二区三区精品 | 亚洲自拍中文 | 九九九九九热 | 91精品人妻一区二区三区蜜桃2 | 91在线中文字幕 | 浪潮av色 | 日韩性生活大片 | 美女无遮挡免费网站 | 国产中文网| 成人区视频 | 亚洲欧美日韩精品永久在线 | 欧美三日本三级少妇99 | 久久精品影视 | 黄色录像网址 | 日本天堂网在线 | 婷婷的五月 | 国产小视频在线免费观看 | 欧美日韩色图 | 亚洲一区免费电影 | 美国成人免费视频 | 奇米影视在线 | 日韩欧美中文字幕一区二区 | 亚洲国产精一区二区三区性色 | 久99精品 | 夜夜爽夜夜爽 | 欧美在线免费观看视频 | 国产女人18毛片18精品 | 国产第一精品视频 | 中文字幕人妻一区二 | 综合色导航 | aaa一级黄色片 | 精品久久国产 | 成人av激情 | 美女精品视频 | 封神榜二在线高清免费观看 | 久久99久久99精品免视看婷婷 | 欧美视频www| 人人操天天射 | 久久九九久精品国产免费直播 | 欧美性猛交一区二区三区精品 | 91精品国产麻豆国产自产在线 | a级片在线 | 91精品视频网 | 欧美日韩中文视频 | 69视频一区二区 | 红桃视频一区二区三区免费 | 四虎国产精品永久在线国在线 | 成人精品毛片 | 久久不射电影网 | 日本色视| 久久频| 亚洲一区二区三区婷婷 | 欧美韩一区二区 | 人妻精品久久久久中文字幕69 | 亚洲欧美日韩国产 | 大色综合| 天天色天天色 | 韩国av一区| 欧美精品免费播放 | 日美av| 电影《两个尼姑》免费播放 | 亚洲网址在线 | 一级片日韩 | 不卡av电影在线观看 | 国产综合久久 | 亚洲免费三区 | 4438x亚洲最大| 骚虎视频在线观看 | 韩国av一区 | 一区精品视频在线观看 | 干美女视频 | 日本久久久久久久久久 | 国产欧美精品 | 桃谷绘里香在线播放 | 欧美性猛交ⅹ乱大交3 | 岛国久久久| 欧美大片大全 |