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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

STL 之replace,replace_if,replace_copy,replace_copy_if

發(fā)布時間:2024/4/11 编程问答 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 STL 之replace,replace_if,replace_copy,replace_copy_if 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
作用:用一個新值替換指定區(qū)間內(nèi)所有的指定元素。

聲明:

  • #include?<algorithm>??
  • template?<class?forwardItr,class?Type>??
  • void?replace(forwardItr?first,?forwardItr?last,const?Type&?oldValue?const?Type&?newValue);??
  • ??
  • template?<class?forwardItr,?class?unaryPredicate,class?Type>??
  • void?replace_if(forwardItr?first,?forwardItr?last,?unaryPredicate?op,const?Type&?newValue);??
  • ??
  • template?<class?inputItr,class?outputItr,class?Type>??
  • outputItr?replace_copy(inputItr?first1,?inputItr?last1,?outputItr?destFirst,const?Type&?oldValue,?const?Type&?newValue);??
  • ??
  • template?<class?inputItr,class?outputItr,?class?unaryPredicate>??
  • outputItr?replace_copy_if(inputItr?first1,?inputItr?last1,?outputItr?destFirst,?unaryPredicate?op,const?Type&?newValue);??

  • 示例代碼:

  • #include?<iostream>??
  • #include?<list>??
  • ??
  • #include?<string>??
  • #include?<numeric>??
  • #include?<iterator>??
  • #include?<vector>??
  • #include?<functional>??
  • ??
  • #include?<algorithm>??
  • ??
  • using?namespace?std;??
  • ??
  • bool?lessThanEqual50(int?num)?{??
  • ????return?(num?<=?50);??
  • }??
  • ??
  • int?main()?{??
  • ????char?cList[10]?=?{'A','a','A','B','A','c','D','e','F','A'};??
  • ????vector<char>?charList(cList,cList+10);??
  • ????ostream_iterator<char>?screen(cout,?"?");??
  • ??
  • ????cout?<<?"charList:"?<<?endl;??
  • ????copy(charList.begin(),charList.end(),screen);??
  • ????cout?<<?endl;??
  • ????//?replace??
  • ????//?將容器中的A替換為Z??
  • ????replace(charList.begin(),charList.end(),'A','Z');??
  • ????cout?<<?"charList.replace?A?->?Z:"?<<?endl;??
  • ????copy(charList.begin(),charList.end(),screen);??
  • ????cout?<<?endl;??
  • ????//?replace_if??
  • ????//?將所有的大寫字母替換為*??
  • ????replace_if(charList.begin(),charList.end(),isupper,'*');??
  • ????cout?<<?"charList.replace_if?Upper->*"?<<?endl;??
  • ????copy(charList.begin(),charList.end(),screen);??
  • ????cout?<<?endl;??
  • ??
  • ????int?listi[10]?=?{12,34,56,21,34,78,34,55,12,25};??
  • ????vector<int>?intList(listi,listi+10);??
  • ????ostream_iterator<int>?screenInt(cout,?"?");??
  • ????cout?<<?"intList:"?<<?endl;??
  • ????copy(intList.begin(),intList.end(),screenInt);??
  • ????cout?<<?endl;??
  • ??
  • ????vector<int>?temp1(10);??
  • ????//?將intList中34全部替換為0,并輸出到temp1中,不改變intList??
  • ????replace_copy(intList.begin(),intList.end(),temp1.begin(),34,0);??
  • ????cout?<<?"intList.replace_copy:"?<<?endl;??
  • ????copy(intList.begin(),intList.end(),screenInt);??
  • ????cout?<<?endl;??
  • ????cout?<<?"temp1:"?<<?endl;??
  • ????copy(temp1.begin(),temp1.end(),screenInt);??
  • ????cout?<<?endl;??
  • ??
  • ????vector<int>?temp2(10);??
  • ????//?將intList中小于50的全部替換為50,并輸出到temp2中,不改變intList??
  • ????replace_copy_if(intList.begin(),intList.end(),temp2.begin(),lessThanEqual50,50);??
  • ????cout?<<?"intList.replace_copy_if:"?<<?endl;??
  • ????copy(intList.begin(),intList.end(),screenInt);??
  • ????cout?<<?endl;??
  • ????cout?<<?"temp2:"?<<?endl;??
  • ????copy(temp2.begin(),temp2.end(),screenInt);??
  • ????cout?<<?endl;??
  • ??
  • ????return?0;??
  • }??

  • 運(yùn)行結(jié)果:

    charList:
    A a A B A c D e F A
    charList.replace A -> Z:
    Z a Z B Z c D e F Z
    charList.replace_if Upper->*
    * a * * * c * e * *
    intList:
    12 34 56 21 34 78 34 55 12 25
    intList.replace_copy:
    12 34 56 21 34 78 34 55 12 25
    temp1:
    12 0 56 21 0 78 0 55 12 25
    intList.replace_copy_if:
    12 34 56 21 34 78 34 55 12 25
    temp2:
    50 50 56 50 50 78 50 55 50 50

    總結(jié)

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

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