2013-04-22 71 views
1

我知道我能做到这一点:调用内部STD功能:: all_of

vector<int> insidetest; 

if(std::all_of(insidetest.begin(),insidetest.end(),[](int i){return i>100;})) 
    { 
     std::cout << "All greater" << std::endl; 
    } 

但我想调用其他职能(也许不仅仅是> 1000更复杂)。我如何可以调用的std :: all_of内的另一个功能,例如:

bool fun(const vector<int> *s) 
    { 
    return true; 
    } 
+2

目前尚不清楚你想要做什么。传递给'std :: all_of'的参数将是'typename Container :: value_type'(在本例中为'int'),所以使用一个带有'const向量 * s'的函数是没有意义的。 – Yuushi 2013-04-22 06:04:42

+0

你是不是指在传递给'std :: all_of'的lambda中调用'fun'? – 2013-04-22 06:06:25

+0

这是我的错误。我读到:“谓词函数的签名应该等价于:bool pred(const Type&a)”,所以在我的情况下,我认为它是const vector *。现在我明白它是如何工作的 – 2013-04-22 06:08:52

回答

8

如果fun有这样的签名 - 也没有办法。 它fun有签名bool(int)然后简单地写

if(std::all_of(insidetest.begin(),insidetest.end(),fun)) 

如果你想在功能其他参数 - 您可以使用std::bind 例如签名bool(int, int, int)

bool fun(int value, int min, int max) 
{ 
    return value > min && value < max; 
} 

if(std::all_of(insidetest.begin(),insidetest.end(), 
       std::bind(fun, std::placeholders::_1, 1, 5)))