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.
";
    
    // counting elements in container:
    vector<int> myvector (myints, myints+8);
    mycount = count(myvector.begin()+2, myvector.end(), 20);
    cout << "20 appears " << mycount  << " times.
";
    
    return 0;
}

原文地址:https://www.cnblogs.com/sea-stream/p/9816215.html