【问题标题】:Function for calculating the vowels in a string计算字符串中元音的函数
【发布时间】:2020-02-19 09:47:17
【问题描述】:

我写了这个函数。但是老师告诉我std::count_if函数的第3个rd参数中需要通过lambda来判断字母是否为元音。

我不知道如何将它转移到那里。

unsigned CalculateVowels(const std::string& str)
{
    const std::string& vowels = "aeiouAEIOU";
    unsigned count = std::count_if(str.begin(), str.end(), [](int index) {return str[index] == vowels[index]; })

    return count;
}

【问题讨论】:

  • Vowels in string C++的可能重复
  • @ibnelaiq 我的问题有解决方案吗?不不......他们不使用count_if函数。
  • 你需要将int index改为char letter然后返回:std::find(vowels.begin(), vowels.end(), letter) != v.end()
  • 谓词应该采用元素而不是索引(见答案)
  • @oksanavovl,你应该使用std::string::find() 而不是std::find(),例如:size_t count = std::count_if(str.begin(), str.end(), [&vowels](char ch) { return vowels.find(ch) != std::string;;npos; });

标签: c++ algorithm c++11 lambda stdstring


【解决方案1】:

你的 lambda 函数是错误的。

它需要检查来自str 的当前元素是否匹配vowels 中的任何元素。为此,您可以使用 <algorithm> 标头中的标准算法 std::any_of

#include <algorithm> // std::any_of, std::count_if

auto CalculateVowels(const std::string& str)
{
    const std::string& vowels = "aeiouAEIOU";
    return std::count_if(
        str.cbegin(), // for each elements in the elements of passed `str`
        str.cend(), 
        [&vowels](const char element) 
        {
            // following checks `std::any_of` the `vowels` element
            // matches the element in the passed `str`
            return std::any_of(
                vowels.cbegin(), 
                vowels.cend(), 
                [element](const char vow) { return vow == element; }
            );

        }
    );
}

(See live online)


如果一行太多了,就把它分成小块。

#include <algorithm> // std::find, std::count_if 

auto CalculateVowels(const std::string& str)
{
    const std::string& vowels = "aeiouAEIOU";
    // lambda to check whether passed `char` element is a match
    // of any of the `vowels`
    const auto isVowel = [&vowels](const char element_to_be_checked)
    {
        return std::any_of(
            vowels.cbegin(),
            vowels.cend(),
            [element_to_be_checked](const char vow)
            {
                return vow == element_to_be_checked;
            }
        );
    };
    // now simply `std::count_if` the element `isVowel`
    return std::count_if(str.cbegin(), str.cend(), isVowel);
}

或者像 @DimChtz 试图在 cmets 中解释,使用 std::find 甚至像 @RemyLebeau 建议的那样更好,使用 std::string::find

#include <string>    // std::string::find
#include <algorithm> // std::find, std::count_if 

auto CalculateVowels(const std::string& str)
{
    const std::string& vowels = "aeiouAEIOU";
    const auto isVowel = [&vowels](const char element_to_be_checked)
    {
        // return std::find(vowels.cbegin(), vowels.cend(), element_to_be_checked) != vowels.cend();
        // or using `std::string::find`
        return vowels.find(element_to_be_checked) != std::string::npos;
    };
    return std::count_if(str.cbegin(), str.cend(), isVowel);
}

(See live online)

【讨论】:

  • std::count_if(..., [isVowel](const char element) { return isVowel(element); } ); 应该是 std::count_if(..., isVowel); 我会使用 std::string::find() 而不是 std::find()
猜你喜欢
  • 2011-09-05
  • 1970-01-01
  • 1970-01-01
  • 2013-11-26
  • 1970-01-01
  • 2018-04-30
  • 2022-08-13
  • 2021-06-09
  • 1970-01-01
相关资源
最近更新 更多