我正在搜索一个可以计算vector
中所有正数的函数!我需要你的帮助。到目前为止,我发现的唯一函数是来自std::count()
的{{1}},但它只在容器中搜索相当于某个值的元素。也许有一种方法可以让这个函数搜索某个范围内的匹配(在我的情况下,这个范围将从1到+无穷大)?感谢。
答案 0 :(得分:12)
最接近的是std::count_if
。
你可以这样使用它:
#include <algorithm>
#include <vector>
int count_bigger(const std::vector<int>& elems) {
return std::count_if(elems.begin(), elems.end(), [](int c){return c > 0;});
}