c++ 對符合條件的元素進行計數(count_if)
阿新 • • 發佈:2018-11-10
#include <iostream> // cout #include <algorithm> // count_if #include <vector> // vector using namespace std; bool IsOdd (int i) { return ((i%2)!=0); } int main () { vector<int> myvector; for (int i=1; i<10; i++) myvector.push_back(i); // myvector: 1 2 3 4 5 6 7 8 9int mycount = count_if (myvector.begin(), myvector.end(), IsOdd); cout << "myvector contains " << mycount << " odd values.\n"; return 0; }