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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

STL 之remove,remove_if,remove_copy,remove_copy_if

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

作用:從序列中刪除指定元素。

聲明:

  • #include?<algorithm>??
  • template?<class?forwardItr,class?Type>??
  • forwardItr?remove(forwardItr?first,?forwardItr?last,?const?Type&?value);??
  • ??
  • template?<class?forwardItr,?class?unaryPredicate>??
  • forwardItr?remove_if(forwardItr?first,?forwardItr?last,?unaryPredicate?op);??
  • ??
  • template?<class?inputItr,class?outputItr,class?Type>??
  • outputItr?remove_copy(inputItr?first1,?inputItr?last1,?outputItr?destFirst,?const?Type&?value);??
  • ??
  • template?<class?inputItr,class?outputItr,?class?unaryPredicate>??
  • outputItr?remove_copy_if(inputItr?first1,?inputItr?last1,?outputItr?destFirst,?unaryPredicate?op);??

  • 示例代碼:

  • #include?<iostream>??
  • #include?<list>??
  • ??
  • #include?<string>??
  • #include?<numeric>??
  • #include?<iterator>??
  • #include?<vector>??
  • #include?<functional>??
  • ??
  • #include?<algorithm>??
  • ??
  • using?namespace?std;??
  • ??
  • bool?lessThanEqualTo50(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);??
  • ????vector<char>::iterator?lastElem,newLastElem;??
  • ????ostream_iterator<char>?screen(cout,?"?");??
  • ??
  • ????cout?<<?"charList:"?<<?endl;??
  • ????copy(charList.begin(),charList.end(),screen);??
  • ????cout?<<?endl;??
  • ????//?remove:去除容器中的所有的A??
  • ????//?函數返回新區間最后一個元素的下一個位置??
  • ????lastElem?=?remove(charList.begin(),charList.end(),'A');??
  • ????cout?<<?"charList?remove?A:"?<<?endl;??
  • ????copy(charList.begin(),lastElem,screen);??
  • ????cout?<<?endl;??
  • ??
  • ????//?remove_if:去掉所有的大寫字母??
  • ????//?返回新區間的最后一個元素的下一個位置。??
  • ????newLastElem?=?remove_if(charList.begin(),charList.end(),isupper);??
  • ????cout?<<?"charList?remove?if?Upper:"?<<?endl;??
  • ????//?顯示新區間的所有元素??
  • ????copy(charList.begin(),newLastElem,screen);??
  • ????cout?<<?endl;??
  • ??
  • ????int?list[10]?=?{12,34,56,21,34,78,34,55,12,25};??
  • ????vector<int>?intList(list,list+10);??
  • ????vector<int>::iterator?endElement,newEndElement;??
  • ????ostream_iterator<int>?screenInt(cout,?"?");??
  • ??
  • ????cout?<<?"intList:"?<<?endl;??
  • ????copy(intList.begin(),intList.end(),screenInt);??
  • ????cout?<<?endl;??
  • ??
  • ????vector<int>?temp1(10);??
  • ????//?將?intList?中除值為34以外的元素,輸出到temp1中,不改變?intList。??
  • ????endElement?=?remove_copy(intList.begin(),intList.end(),temp1.begin(),34);??
  • ????cout?<<?"temp1:"?<<?endl;??
  • ????copy(temp1.begin(),endElement,screenInt);??
  • ????cout?<<?endl;??
  • ????cout?<<?"intList:"?<<?endl;??
  • ????copy(intList.begin(),intList.end(),screenInt);??
  • ????cout?<<?endl;??
  • ??
  • ????vector<int>?temp2(10,0);??
  • ????//?remove_copy_if??
  • ????//?將intList中大于50的元素輸送到?temp2中。??
  • ????newEndElement?=?remove_copy_if(intList.begin(),intList.end(),temp2.begin(),lessThanEqualTo50);??
  • ????cout?<<?"temp2:"?<<?endl;??
  • ????copy(temp2.begin(),temp2.end(),screenInt);??
  • ????cout?<<?endl;??
  • ??
  • ????cout?<<?"temp2:"?<<?endl;??
  • ????copy(temp2.begin(),newEndElement,screenInt);??
  • ????cout?<<?endl;??
  • ??
  • ????return?0;??
  • }??
  • 運行結果:

    charList:
    A a A B A c D e F A
    charList remove A:
    a B c D e F
    charList remove if Upper:
    a c e e
    intList:
    12 34 56 21 34 78 34 55 12 25
    temp1:
    12 56 21 78 55 12 25
    intList:
    12 34 56 21 34 78 34 55 12 25
    temp2:
    56 78 55 0 0 0 0 0 0 0
    temp2:
    56 78 55

    總結

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

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