C++11——std::all_of,

翻译来自: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