c++ 在指定長度的數組或者容器中,統計元素出現的次數(count)


 

#include <iostream>     // cout
#include <algorithm>    // count
#include <vector>       // vector
using namespace std;
int main () {
    // counting elements in array:
    int myints[] = {10,20,30,30,20,10,10,20};   // 8 elements
    int mycount = count(myints, myints+6, 10);
    cout << "10 appears " << mycount << " times.\n";
    
    // counting elements in container:
    vector<int> myvector (myints, myints+8);
    mycount = count(myvector.begin()+2, myvector.end(), 20);
    cout << "20 appears " << mycount  << " times.\n";
    
    return 0;
}

 


免責聲明!

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



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