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

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

STL 之for_each,transform

發(fā)布時間:2024/4/11 编程问答 38 豆豆
生活随笔 收集整理的這篇文章主要介紹了 STL 之for_each,transform 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
for_each:對指定區(qū)間中的每個元素使用指定的函數(shù)進(jìn)行訪問及處理,所用的函數(shù)作為參數(shù)傳遞給該函數(shù)。

transform:元素傳輸


聲明:

  • #include?<algorithm>??
  • template?<class?inputItr,class?function>??
  • function?for_each(inputItr?first,inputItr?last,function?func);??
  • ??
  • template?<class?inputItr,?class?outputItr,?class?unaryOperation>??
  • outputItr?transform(inputItr?first,inputItr?last,?outputItr?destFirst,unaryOperation?op);??
  • ??
  • template?<class?inputItr1,?class?inputItr2,?class?outputItr,?class?binaryOperation>??
  • outputItr?transform(inputItr1?first1,?inputItr1?last,?inputItr2?first2,?outputItr?destFirst,binaryOperation?bop);??

  • 示例代碼:

  • #include?<iostream>??
  • #include?<list>??
  • ??
  • #include?<string>??
  • #include?<numeric>??
  • #include?<iterator>??
  • #include?<vector>??
  • #include?<functional>??
  • ??
  • #include?<algorithm>??
  • ??
  • using?namespace?std;??
  • ??
  • void?doubleNum(int&?num)?{??
  • ????num?=?2?*?num;??
  • ????cout?<<?num?<<?"?";??
  • }??
  • ??
  • int?main()?{??
  • ????char?cList[5]?=?{'a','b','c','d','e'};??
  • ????vector<char>?charList(cList,cList+5);??
  • ????ostream_iterator<char>?sreen(cout,?"?");??
  • ??
  • ????cout?<<?"charList:"?<<?endl;??
  • ????copy(charList.begin(),charList.end(),sreen);??
  • ????cout?<<?endl;??
  • ??
  • ????//transform??
  • ????transform(charList.begin(),charList.end(),charList.begin(),toupper);??
  • ????cout?<<?"charList:"?<<?endl;??
  • ????copy(charList.begin(),charList.end(),sreen);??
  • ????cout?<<?endl;??
  • ??
  • ????int?list[7]?=?{2,8,5,1,7,11,3};??
  • ????ostream_iterator<int>?srceenInt(cout,?"?");??
  • ????cout?<<?"list"?<<?endl;??
  • ????copy(list,list+7,srceenInt);??
  • ????cout?<<?endl;??
  • ??
  • ????//?for_each??
  • ????for_each(list,list?+?7,?doubleNum);??
  • ????cout?<<?endl;??
  • ??
  • ????cout?<<?"list"?<<?endl;??
  • ????copy(list,list+7,srceenInt);??
  • ????cout?<<?endl;??
  • ??????
  • ????return?0;??
  • }??
  • 運(yùn)行結(jié)果:

    charList:
    a b c d e
    charList:
    A B C D E
    list
    2 8 5 1 7 11 3
    4 16 10 2 14 22 6
    list
    4 16 10 2 14 22 6

    總結(jié)

    以上是生活随笔為你收集整理的STL 之for_each,transform的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。