C++ 遍历字符串和switch语句:C++;
我在写一些代码,遇到了一些麻烦。我想写一个函数来检查字符串是否有元音,并试图通过一个包含switch语句的for循环来完成。显然,它不起作用,而且由于某种原因永远不会返回真值C++ 遍历字符串和switch语句:C++;,c++,C++,我在写一些代码,遇到了一些麻烦。我想写一个函数来检查字符串是否有元音,并试图通过一个包含switch语句的for循环来完成。显然,它不起作用,而且由于某种原因永远不会返回真值 bool scanStr(string userInp) { for (int i = 0; i < userInp.size(); i++) { switch (userInp[i]) { case 'a': case 'A':
bool scanStr(string userInp) {
for (int i = 0; i < userInp.size(); i++) {
switch (userInp[i])
{
case 'a':
case 'A':
case 'e':
case 'E':
case 'i':
case 'I':
case 'o':
case 'O':
case 'u':
case 'U':
case 'y':
case 'Y':
return true;
break;
default:
return false;
}
}
}
boolscanstr(字符串userInp){
对于(int i=0;i
我试着测试程序是否真的在遍历字符串,确实如此,所以我不明白为什么在函数中,它总是返回false
int main() {
string userInp;
string pigLatin;
cout << "Please enter a string to convert to pig Latin: " << endl;
cin >> userInp;
cout << endl;
// tests
for (int i = 0; i < userInp.size(); i++) { //checking if it actually iterates
cout << userInp[i];
}
cout << endl;
if (scanStr(userInp))
cout << "it has a vowel" << endl;
else
cout << "no vowel" << endl;
system("pause");
return 0;
}
intmain(){
字符串userInp;
拉丁语;
coutuserinp;
不能从函数中删除以下行:
default:
return false;
bool is_vowel(char x)
{
switch (x)
{
case 'a':
case 'A':
case 'e':
case 'E':
case 'i':
case 'I':
case 'o':
case 'O':
case 'u':
case 'U':
case 'y':
case 'Y':
return true;
default:
return false;
}
}
它们使函数在遇到第一个非元音时返回false
如果到达循环的末尾并且尚未返回true
,则只希望返回false
bool scanStr(string userInp)
{
for (int i = 0; i < userInp.size(); i++)
{
switch (userInp[i])
{
case 'a':
case 'A':
case 'e':
case 'E':
case 'i':
case 'I':
case 'o':
case 'O':
case 'u':
case 'U':
case 'y':
case 'Y':
return true;
}
}
return false;
}
<>但是如果你不知道它的意思,现在不要担心,你的书或教程很快就会解释。 问题是,如果任何字符不是元音,函数就被错误地返回了。也使用<代码> const和<代码> > <代码> const 允许你传递const字符串,引用节省一些时间,因为C++不需要C。整根绳子
bool scanStr(const string & userInp) {
for (int i = 0; i < userInp.size(); i++) {
switch (userInp[i])
{
case 'a':
case 'A':
case 'e':
case 'E':
case 'i':
case 'I':
case 'o':
case 'O':
case 'u':
case 'U':
case 'y':
case 'Y':
return true;
break;
}
}
return false;
}
boolscanstr(常量字符串和userInp){
对于(int i=0;i
我建议您将元音测试的逻辑提取到它自己的功能中:
default:
return false;
bool is_vowel(char x)
{
switch (x)
{
case 'a':
case 'A':
case 'e':
case 'E':
case 'i':
case 'I':
case 'o':
case 'O':
case 'u':
case 'U':
case 'y':
case 'Y':
return true;
default:
return false;
}
}
然后可以使用标准算法而不是循环:
#include <algorithm>
#include <string>
bool contains_vowel(const std::string& str)
{
return std::any_of(str.begin(), str.end(), is_vowel);
}
#包括
#包括
布尔包含_元音(常量std::string和str)
{
return std::(str.begin(),str.end(),is_元音)的任意_;
}
(我将scanStr
重命名为包含_元音
,因为该名称更具描述性。)首先编译器将开关大小写转换为查找表,然后编译器将根据指定的值(在本例中为字符
)将自动
确定为数据类型
或者把它交给你的编译器,它知道怎么做
您的代码错误。我将为您修复您的循环计数器I
的类型应为std::size\u t
,因为userInp.size()
可能并不总是适合有符号的int
。还要注意返回true;
语句之后的break;
语句是死代码,永远不会执行。请阅读std::string::find\u first\u of
。我很困惑,假设我有一个字符串“Hello”,使用H作为第一次迭代,在我的代码中,它会变成默认值并返回false,但是没有break语句,所以代码不应该继续吗?@Panthy否,一旦return
函数结束并返回值(如果有)是返回给调用者的。我明白了……假设我有一个变量bool X,而不是返回true/false,我将true/false的值放入X中,最后将X从循环中返回,假设这也会起作用吗?@Panthy:您可以退出一个包含所有流控制语句的循环(除了“continue”):break
,goto
,return
(除了break
在这种情况下不起作用,因为它会从开关中断开。)假设你不犯错误,那是可以的。这会更复杂,容易出错。试试看,这可能是一个很好的练习。我不知道,对C++来说还是新的,所以我不熟悉所有的函数,这是C函数,但它是纯设计的东西。它会省去你的一些代码。但是如果你熟悉C和指针,那么引用比C++中的指针更为重要。到目前为止,我最近能把引用弄得乱七八糟的是“符号”,但是在我看来,C++看起来更复杂了:PIT其实很简单。这里有徽章…“没有性能问题”我希望切换速度更快。这里完全丢失了,但我有点理解!“字符串中的所有字符都是元音吗?”只是“序列中的所有元素都满足某个谓词吗?”C++标准LabBy提供了一个算法,因为它是唯一的答案,以促进解耦和使用STD算法。