gpt4 book ai didi

C++ 如何使用 find 函数在 char 数组中查找 char?

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:08:55 25 4
gpt4 key购买 nike

如何使用 find 函数在 char 数组中查找 char?如果我只是循环元音,那么我可以得到答案,但我被要求使用 std::find.. 谢谢。

bool IsVowel (char c) { 

char vowel[] = {'a', 'e', 'i', 'o', 'u'};
bool rtn = std::find(vowel, vowel + 5, c);

std::cout << " Trace : " << c << " " << rtn << endl;

return rtn;
}

最佳答案

bool IsVowel (char c) { 

char vowel[] = {'a', 'e', 'i', 'o', 'u'};
char* end = vowel + sizeof(vowel) / sizeof(vowel[0]);
char* position = std::find(vowel, end, c);

return (position != end);
}

关于C++ 如何使用 find 函数在 char 数组中查找 char?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4060210/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com