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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

STL 之search,search_n,sort,binary_search

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

作用:用來查找元素和元素排序


?聲明:

  • #include?<algorithm>??
  • template?<class?forwardItr1,?class?forwardItr2>??
  • forwardItr1?search(forwardItr1?first1,?forwardItr1?last1,forwardItr2?first2,forwardItr2?last2);??
  • template?<class?forwardItr1,?class?forwardItr2,class?binaryPredicate>??
  • forwardItr1?search(forwardItr1?first1,?forwardItr1?last1,forwardItr2?first2,forwardItr2?last2,binaryPredicate?op);??
  • ??
  • template?<class?forwardItr,?class?size,class?Type>??
  • forwardItr?search_n(forwardItr?first,?forwardItr?last,size?count,const?Type&?value);??
  • template?<class?forwardItr,?class?size,class?Type,class?binaryPredicate>??
  • forwardItr?search_n(forwardItr?first,?forwardItr?last,size?count,const?Type&?value,binaryPredicate?op);??
  • ??
  • template<class?randomAccessItr>??
  • void?sort(randomAccessItr?first,randomAccessItr?last);??
  • template<class?randomAccessItr,?class?compare>??
  • void?sort(randomAccessItr?first,?randomAccessItr?last,?compare?op);??
  • ??
  • template<class?forwardItr,class?Type>??
  • bool?binary_search(forwardItr?first,forwardItr?last,const?Type&?searchValue);??
  • template<class?forwardItr,class?Type,class?compare>??
  • bool?binary_search(forwardItr?first,?forwardItr?last,?const?Type&?searchValue,compare?op);??

  • 示例代碼:

  • #include?<iostream>??
  • #include?<list>??
  • ??
  • #include?<string>??
  • #include?<numeric>??
  • #include?<iterator>??
  • #include?<vector>??
  • #include?<functional>??
  • ??
  • #include?<algorithm>??
  • ??
  • using?namespace?std;??
  • ??
  • int?main()?{??
  • ????int?intList[15]?=?{12,34,56,34,34,??
  • ???????????????????????78,38,43,12,25,??
  • ???????????????????????34,56,62,5,49};??
  • ????vector<int>?vecList(intList,intList+15);??
  • ????int?list[2]?=?{34,?56};??
  • ????vector<int>::iterator?location;??
  • ??
  • ????ostream_iterator<int>?screen(cout,?"?");??
  • ??
  • ????cout?<<?"vecList:"?<<?endl;??
  • ????copy(vecList.begin(),vecList.end(),screen);??
  • ????cout?<<?endl;??
  • ????cout?<<?"list:"?<<?endl;??
  • ????copy(list,list+2,screen);??
  • ????cout?<<?endl;??
  • ??
  • ????//?search:查找一個集合是否在另一集合中??
  • ????//?與find的區別是,find是查找某一個元素??
  • ????location?=?search(vecList.begin(),vecList.end(),list,list?+?2);??
  • ????if?(location?!=?vecList.end())??
  • ????{??
  • ????????cout?<<?"location:"?<<?(location?-?vecList.begin())?<<?endl;??
  • ????}?else?{??
  • ????????cout?<<?"list?is?not?in?vecList"?<<?endl;??
  • ????}??
  • ????//?search_n?:查找某個元素第n此出現的位置??
  • ????location?=?search_n(vecList.begin(),vecList.end(),2,34);??
  • ????if?(location?!=?vecList.end())??
  • ????{??
  • ????????cout?<<?"location:"?<<?(location?-?vecList.begin())?<<?endl;??
  • ????}?else?{??
  • ????????cout?<<?"list?is?not?in?vecList"?<<?endl;??
  • ????}??
  • ??
  • ????//?sort??
  • ????sort(vecList.begin(),vecList.end());??
  • ????cout?<<?"vecList:"?<<?endl;??
  • ????copy(vecList.begin(),vecList.end(),screen);??
  • ????cout?<<?endl;??
  • ??
  • ????bool?found;??
  • ????//?用二分法查找:前提先排序??
  • ????found?=?binary_search(vecList.begin(),vecList.end(),43);??
  • ??
  • ????if?(found)??
  • ????{??
  • ????????cout?<<?"43?found?in?vecList."?<<?endl;??
  • ????}?else?{??
  • ????????cout?<<?"43?not?found?in?vecList."?<<?endl;??
  • ????}??
  • ??
  • ????return?0;??
  • }??

  • 運行結果:

    vecList:
    12 34 56 34 34 78 38 43 12 25 34 56 62 5 49
    list:
    34 56
    location:1
    location:3
    vecList:
    5 12 12 25 34 34 34 34 38 43 49 56 56 62 78
    43 found in vecList.

    總結

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

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