Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/125.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 有一个函数可以计算向量中所有的正数吗?_C++_Stl Algorithm - Fatal编程技术网

C++ 有一个函数可以计算向量中所有的正数吗?

C++ 有一个函数可以计算向量中所有的正数吗?,c++,stl-algorithm,C++,Stl Algorithm,我正在搜索一个函数,它可以计算向量中的所有正数!我需要你的帮助。到目前为止,我找到的唯一函数是算法中的std::count(),但它只在容器中搜索与某个值相等的元素。也许有一种方法可以让这个函数搜索特定范围内的匹配项(在我的例子中,这个范围是从1到+无穷大)?谢谢。最接近的是std::count\u if 您可以这样使用它: #include <algorithm> #include <vector> int count_bigger(const std::vector

我正在搜索一个函数,它可以计算
向量中的所有正数
!我需要你的帮助。到目前为止,我找到的唯一函数是
算法中的
std::count()
,但它只在容器中搜索与某个值相等的元素。也许有一种方法可以让这个函数搜索特定范围内的匹配项(在我的例子中,这个范围是从1到+无穷大)?谢谢。

最接近的是
std::count\u 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;});
}
#包括
#包括
整数计数(常数标准::向量和元素){
返回std::count_if(elems.begin(),elems.end(),[](int c){return c>0;});
}

您非常接近您的解决方案:如果(v.begin()、v.end()、std::bind2nd(std::greater()、0))
@milleniumbug,则有
std::count\u,非常感谢!我想我不知怎么跳过了。。。