【发布时间】:2021-01-15 19:07:59
【问题描述】:
我正在编写一些代码来计算给定单词中元音的数量,我已经完成了一些磨房代码,但是我想知道是否可以按照这些思路做一些事情
bool isVowel(char inputcharacter) //Bool function to check the validity of a character as being a bool
{
set<char> Vowels{'a','e','i','o','u','A','E','I','O','U'}; //Pre-Makes a set of characters (Specifically Vowels) Used to check characters
if (Vowels.find(inputcharacter) != Vowels.end()) //if the character is found within the list, before the list's end, the return will be true (Theoreticle indexing element that extends beyond the "physical" list)
{
return true; //Returns a true value
}
return false; //Returns a false value else
}
int numVowels(string inputstring) //Vowelcounter function
{
int Contained_Vowels = 0; //Intializes the counter to be 0
for (char c : inputstring; (isVowel(c) == true); Contained_Vowels++);
return Contained_Vowels;
}
【问题讨论】:
-
您的问题是什么?此外,您的 numVowels 函数不会返回 inputstring 的元音数字,而是返回 inputstring 中连续元音的数字。
-
不,只需在 for 循环的主体中使用 if 语句或使用
std::count_if一起摆脱 for 循环 -
我的问题是,是否可以在 numVowels 中编写一些类似于 for 循环的代码,numVowels 目前不返回任何内容,因为 for 循环有错误。那么有没有办法在for循环中创建一个逻辑类型的电路
-
类似于列表推导,但用于循环推导 Python 列表推导 tempList = [(i[0], i[2]) for i in data if i[1] == 'fruit']
-
类似的东西是
for (char c: inputstring) Contained_Vowels += isVowel(c);。