C ++ How to find char in char array using find function?

How to find char in char array using find function? If I just looped the vowel, then I could get an answer, but I was asked to use std :: find .. Thanks.

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; } 
+4
source share
4 answers
 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); } 
+4
source

std::find(first, last, value) returns an iterator to the first element that matches value in the range [first, last]. If there is no match, it returns last .

In particular, std :: find does not return a boolean value. To get the boolean value you are looking for, you need to compare the return value (without converting it to a boolean first!) Std :: find to last (i.e. if they are equal, no match is found).

+2
source

Simplification and correction

 inline bool IsVowel(char c) { return std::string("aeiou").find(c) != std::string::npos; } 

See demo http://ideone.com/NnimDH .

+1
source

Using:

 size_t find_first_of ( char c, size_t pos = 0 ) const; 

Link: http://www.cplusplus.com/reference/string/string/find_first_of/

0
source

All Articles