Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the most efficient way to search a set in c++

Tags:

c++

For example to traverse a string and count the number of vowels I could use

  for (int i=0; (x=inputString[i])!='\0';i++)
    if (x=='a' || x=='e' || x=='i' || x=='o' || x=='u') numVowels++;

In functional languages like haskell or erlang we can do lists:member(x,"aeiou")

In some object oriented languages I can do something like ["a","e","i","o","u"].find(x)

What's the cleanest way to do this in C++ without the x==.. || x==.. || ...

like image 512
BreezyChick89 Avatar asked Nov 23 '25 11:11

BreezyChick89


1 Answers

inputString.find_first_of("aeiou");

This will give you the first index of any matching element. You can do this in loop to get all matching element:

size_t idx=0;
do{
    size_t current = inputString.find_first_of("aeiou",idx);
    idx = current;
}
while(current!=string::npos);
like image 94
deeiip Avatar answered Nov 25 '25 02:11

deeiip