使用std::count
来自<algorithm>
:
std::count(array.begin(), array.end(), 3)
// or if it's a raw array: std::count(array, array + NUM_OF_ELEMENTS, 3)
// or the most generic solution (std::{begin,end} are from C++11):
std::count(std::begin(array), std::end(array), 3)