温馨提示×

如何结合C++的STL容器使用islower

c++
小樊
82
2024-08-30 03:10:35
栏目: 编程语言

islower 是一个函数,用于检查给定字符是否为小写字母

#include<iostream>
#include<string>
#include<algorithm>
#include <cctype>

int main() {
    std::string input = "Hello, World!";
    
    // 使用 std::count_if 和 islower 统计小写字母的数量
    int lowercase_count = std::count_if(input.begin(), input.end(), [](unsigned char c) { return std::islower(c); });

    std::cout << "Number of lowercase letters: "<< lowercase_count<< std::endl;

    return 0;
}

在这个示例中,我们首先包含了必要的头文件,然后创建了一个 std::string 类型的变量 input。接下来,我们使用 std::count_if 算法统计 input 中小写字母的数量。std::count_if 接受两个迭代器(表示要处理的范围)以及一个 lambda 函数,该函数将应用于范围内的每个元素。在这种情况下,我们使用 std::islower 函数检查每个字符是否为小写字母。最后,我们输出小写字母的数量。

0