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

标签 c++ arrays char

如何使用 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/

相关文章:

java - ArrayException越界

java - 将空格转换为下划线

c - 为什么不能使用char指针?

c++ - 通读文件中的字符并计算有多少个字母 [c++]

c++ - 在 C++ 代码中使用 Windows 的 native 程序打开文件

c++ - 结合 static_cast 和 std::any_cast

arrays - 将字符串参数传递给函数以访问数组的属性

c - 有没有办法获取数组的内容并将其存储到指针中?

c++ - 将 wchar_t 转换为 wstring

c++ - "Function not declared in this scope"编译openCV代码出错