so let’s say i have a vector, wich contains 4 elemens [string elements]. I need to loop through the vector first , then through each element , loop through an array of chars [vowels]
and to count how many vowels does that word contain.
for(int i = 0; i < b.size(); i++)
{
for(int j = 0; j < b[i].size(); j++)
{
for(int v = 0 ; v < sizeof( vowels ) / sizeof( vowels[0] ); v++)
if(//blabla)
}
}
so my question is, how can i loop through each word , i mean b[i][j] is the right way to do that?
if yes, this form , will work great? :
if(b[i][j] == vowels[v]) {
//blabla
}
thanks.
A more advanced way to go about this, which you should take a look at if you’re serious about learning C++: don’t use indices and random access, use high-level STL functions. Consider:
Where the loops you’ve written correspond roughly to these lines:
This uses a high-level functional/generic programming idiom that C++ is not always elegant at pulling off IMO. But in this case it works fine.
See Count no of vowels in a string for a rundown of solutions to part of the problem I think you’re trying to solve. You can see a variety of acceptable looping/iteration techniques on display there as well.