可以使用count_if函数来统计容器中满足特定条件的元素个数。
例如,如果要统计一个vector中大于10的元素个数,可以这样写:
#include <iostream>#include <vector>#include <algorithm>int main() { std::vector<int> vec = {5, 10, 15, 20, 25, 30}; int count = std::count_if(vec.begin(), vec.end(), [](int i) { return i > 10; }); std::cout << "大于10的元素个数为: " << count << std::endl; return 0;}运行结果将输出:
大于10的元素个数为: 4 

