c++ count函數


count函數 
algorithm頭文件(#include <algorithm>)定義了一個count的函數,其功能類似於find。這個函數使用一對迭代器和一個值做參數,返回這個值出現次數的統計結果。 
編寫程序讀取一系列int型數據,並將它們存儲到vector對象中,然后統計某個指定的值出現了多少次。 

  • 核心代碼:cout<<count(ivec.begin() , ivec.end() , searchValue)

 

具體實現:

  1. #include <iostream>
  2. #include<vector>
  3. #include<algorithm>
  4. using namespace std;
  5. int main()
  6. {
  7. int a(0); //a用來記錄字母a出現個數
  8. char art[10000]={" "};//初始化為空字符串
  9. cin.getline(art,10000);//輸入字符串
  10. a=count(art, art + 10000, 'a') + count(art, art + 10000, 'A'); //用count函數記錄字符串中'a'和'A'出現次數
  11. cout<<"a字母(不區分大小寫)出現個數"<<a<<endl;//打印輸出
  12. system("pause");
  13. return 0;
  14. }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM