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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

STL 之count,count_if,max,max_element,min,min_element和random_shuffle

發布時間:2024/4/11 编程问答 42 豆豆
生活随笔 收集整理的這篇文章主要介紹了 STL 之count,count_if,max,max_element,min,min_element和random_shuffle 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

count:在指定區間上統計指定值出現的次數。

count_if:條件統計

max:判斷兩個數值中的較大值

max_element:查找指定區間的最大元素

min:判斷兩個數值中的較小值

min_element:查找指定區間的最小元素

random_shuffle:用來將指定區間上的元素按隨機順序排列


聲明:

  • #include?<algorithm>??
  • template?<class?inputItr,class?Type>??
  • iterator_traits<inputItr>::difference_type?count(inputItr?first,inputItr?last,const?Type&?value);??
  • ??
  • template?<class?inputItr,class?unaryPredicate>??
  • iterator_traits<inputItr>::difference_type?count_if(inputItr?first,inputItr?last,unaryPredicate?op);??
  • ??
  • template?<class?Type>??
  • const?Type&?max(const?Type&?aVal,?const?Type&bVal);??
  • template?<class?Type,class?compare>??
  • const?Type&?max(const?Type&?aVal,const?Type&?bVal,compare?comp);??
  • ??
  • template?<class?forwardItr>??
  • forwardItr?max_element(forwardItr?first,forwardItr?last);??
  • template?<class?forwardItr,?class?compare>??
  • forwardItr?max_element(forwardItr?first,forwardItr?last,?compare?comp);??
  • ??
  • template?<class?Type>??
  • const?Type&?min(const?Type&?aVal,?const?Type&bVal);??
  • template?<class?Type,class?compare>??
  • const?Type&?min(const?Type&?aVal,const?Type&?bVal,compare?comp);??
  • ??
  • template?<class?forwardItr>??
  • forwardItr?min_element(forwardItr?first,forwardItr?last);??
  • template?<class?forwardItr,?class?compare>??
  • forwardItr?min_element(forwardItr?first,forwardItr?last,?compare?comp);??
  • ??
  • template?<class?randomAccessItr>??
  • void?random_shuffle(randomAccessItr?first,?randomAccessItr?last);??
  • template?<class?randomAccessItr,?class?randomAccessGenerator>??
  • void?random_shuffle(randomAccessItr?first,randomAccessItr?last,randomAccessGenerator?rand);??

  • 示例代碼:

  • #include?<iostream>??
  • #include?<list>??
  • ??
  • #include?<string>??
  • #include?<numeric>??
  • #include?<iterator>??
  • #include?<vector>??
  • #include?<functional>??
  • ??
  • #include?<algorithm>??
  • ??
  • using?namespace?std;??
  • ??
  • int?main()?{??
  • ????char?cList[10]?=?{'Z','a','Z','B','Z','c','D','e','F','Z'};??
  • ????vector<char>?charList(cList,cList+10);??
  • ????ostream_iterator<char>?screen(cout,"?");??
  • ????cout?<<?"charList:"?<<?endl;??
  • ????copy(charList.begin(),charList.end(),screen);??
  • ????cout?<<?endl;??
  • ??????
  • ????//?count??
  • ????int?noofzs?=?count(charList.begin(),charList.end(),'Z');??
  • ????cout?<<?"count?of?Z?=?"?<<?noofzs?<<?endl;??
  • ??
  • ????//?count_if??
  • ????int?noofupper?=?count_if(charList.begin(),charList.end(),isupper);??
  • ????cout?<<?"count?of?Upper?=?"?<<?noofupper?<<?endl;??
  • ??
  • ??
  • ????int?list[10]?=?{12,34,56,21,34,78,34,55,12,25};??
  • ????ostream_iterator<int>?screenInt(cout,?"?");??
  • ????cout?<<?"list:"?<<?endl;??
  • ????copy(list,list+10,screenInt);??
  • ????cout?<<?endl;??
  • ??
  • ????//?max_element??
  • ????int?*?maxLoc?=?max_element(list,list+10);??
  • ????cout?<<?"the?Largest?element?is?"?<<?*maxLoc?<<?endl;??
  • ??
  • ????//?min_element??
  • ????int?*?minLoc?=?min_element(list,list+10);??
  • ????cout?<<?"the?Smallest?element?is?"?<<?*minLoc?<<?endl;??
  • ??
  • ????//?random_shuffle??
  • ????random_shuffle(list,list+10);??
  • ????cout?<<?"List.random_shuffle"?<<?endl;??
  • ????copy(list,list+10,screenInt);??
  • ????cout?<<?endl;??
  • ??????
  • ????return?0;??
  • }??
  • 運行結果:

    charList:
    Z a Z B Z c D e F Z
    count of Z = 4
    count of Upper = 7
    list:
    12 34 56 21 34 78 34 55 12 25
    the Largest element is 78
    the Smallest element is 12
    List.random_shuffle
    12 34 25 56 12 78 55 21 34 34

    總結

    以上是生活随笔為你收集整理的STL 之count,count_if,max,max_element,min,min_element和random_shuffle的全部內容,希望文章能夠幫你解決所遇到的問題。

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