-
String Help
I recently had to write a program where it should perform a certain action only when there was a letter 'a' in the string. I ended up using something like:
Code:
if (b.find('A')!=npos)
return b;
but my question is, is there a more efficient way to find out if a string contains a certain character? I would have thought that if there is npos there should be something like pos so you could do:
Code:
if (b.find('A') == pos)
return b;
Any help is greatly appreciated.
-
As far as I can see there is no difference in efficiency between the code you wrote and the code you wanted to write, but there is no "pos", npos is generally defined as -1, and find returns -1 if it can't find the character.
If you dont need the function to return the string you could simply write:
return b.find('A') != npos;
that would return true or false.. but depending on your situation you may actually be intending to return b.