2013-04-22 30 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)))