翻譯來自:https://thispointer.com/c11-stdall_of-algorithm-tutorial-example/
在本文中,我們將討論 c++11 中引入的 std::all_of() STL 算法。
需要 std::all_of()
當您有一個元素范圍/數組並且想要檢查給定范圍內的所有元素是否滿足給定條件時,此 STL 算法很有用。它與 std::any_of() 正好相反。
std::all_of() 介紹
all_of() 接受一系列元素和一個一元謂詞(即回調)作為參數。
bool all_of (<Start of Range>, <End of Range>, UnaryPredicate pred);
all_of() 遍歷給定范圍內的所有元素,並為每個元素調用給定的回調,即一元謂詞。
如果對於任何元素,則給定的謂詞返回 false,則停止進一步迭代並返回 false,否則返回 true。
std::all_of() 使用細節
如何將 std::all_of() 與兩種不同類型的回調(即 Lambda 函數和函數指針)一起使用
將 std::all_of() 與 Lambda 函數一起使用
假設我們有一個字符串向量,即
// 一個字符串向量 std::vector < std::string > wordList = { "Hi" , "Hello" , "Test" , "First" , "Second" , "Third" , "Fourth" } ;
現在我們想使用 std::all_of() 檢查向量中的所有字符串是否具有相同的大小
/* Check if all strings in vector are of same size i.e. 4. */ bool result = std::all_of(wordList.begin(), wordList.end(), [](const std::string & str) { return str.size() == 4; });
std::all_of() 將遍歷 vector 中的所有字符串,對於 vector 中的每個字符串,它調用傳遞的 lambda 函數,該函數檢查字符串的大小是否為 4。如果對於任何字符串,則 lambda 函數返回 false,std::all_of () 將停止進一步的迭代並返回 false 否則返回 true。
現在使用 all_of() 檢查向量中的所有字符串是否以大寫字母開頭
/* Check if all strings in vector starts with a Upper Case Letter */ result = std::all_of(wordList.begin(), wordList.end(), [](const std::string & str) { return str.size() > 0 && ::isupper(str[0]); });
將 std::all_of() 與函數指針一起使用
假設我們有一個字符串,即
std::string str = "testString" ;
現在讓我們檢查給定的字符串是否包含所有小寫字母,即不是單個大寫字符
/* Check if given string contains all lower case letters i.e. not a single upper case char */ result = std::all_of(str.begin(), str.end(), ::islower);
將 std::all_of() 與數組一起使用
假設我們有一個數組,即
int arr [] = { 3,9,75,15,12 } ;
現在我們將使用 all_of() 檢查數組中的所有數字是否都是 3 的倍數,即
// 檢查數組中的所有數字是否都是 3 的倍數,即 result = std::all_of(arr, arr + sizeof(arr)/ sizeof(int), [](const int & i){ return i%3 == 0; });
所以,基本上 std::all_of() 是為了讓我們的生活更輕松。
#include <iostream> #include <vector> #include <string> #include <algorithm> int main() { // A vector of strings std::vector<std::string> wordList = { "Hi", "Hello", "Test", "First", "Second", "Third", "Fourth" }; /* Check if all strings in vector are of same size i.e. 4. */ bool result = std::all_of(wordList.begin(), wordList.end(), [](const std::string& str) { return str.size() == 4; }); std::cout << " Is Vector contains all string with size 4 ? | Result = " << result << std::endl; /* Check if all strings in vector starts with a Upper Case Letter */ result = std::all_of(wordList.begin(), wordList.end(), [](const std::string& str) { return str.size() > 0 && ::isupper(str[0]); }); std::cout << "Are all strings in vector starts with a Upper Case Letter ? | Result = " << result << std::endl; std::string str = "testString"; /* Check if given string contains all lower case letters i.e. not a single upper case char */ result = std::all_of(str.begin(), str.end(), ::islower); std::cout << "str = " << str << std::endl; std::cout << "Does given string contains all lower case letters ? | Result = " << result << std::endl; // Using all_of() with array int arr[] = { 3,9,75,15,12 }; // Check if all numbers in array are multiple of 3 i.e. result = std::all_of(arr, arr + sizeof(arr) / sizeof(int), [](const int& i) { return i % 3 == 0; }); std::cout << "Does all numbers in array are multiple of 3 | Result = " << result << std::endl; return 0; }
Is Vector contains all string with size 4 ? | Result = 0 Are all strings in vector starts with a Upper Case Letter ? | Result = 1 str = testString Does given string contains all lower case letters ? | Result = 0 Does all numbers in array are multiple of 3 | Result = 1