- find_first_of() - Find first char in string (public member function)
- finc_first_not_of() - Find first char not in the string
- find_last_of() - Find first char in string from the end
- find_last_not_of() - Find first character not in string from the end
Comparing Strings
Just use == with strings, as if you were comparing two ints. Behind the scenes, the main comparison operator == is defined asbool operator == (const string& left, const string right) ;but a comparison like
if (str =="Hello")could be quite inefficient with this definition because it would have to first convert the char * parameter right to a temporary string object and then copy the value into it. Thankfully there are two overloads which implement a comparison of a string and char * or a char * and string, just to avoid this inefficiency.
The compare function provides more flexibility
This is another overloaded function that allows comparison between parts of strings and char * or other strings. This snippet is from example 4.
const string s("Humpty Dumpty sat on a wall") ;This compares the part of s that starts at position 14 and is 3 chars long with the part of "sat" that starts at position 0 and is also 3 chars long (i.e. "sat") and returns 0 if they match. Don't forget the ==0 or your logic will be inverted and wrong!
if ( s.compare(14,3,"sat",0,3) ==0 )
cout << "Strings Match" << endl;
else
cout << "Strings Don't Match" << endl;
On the next page - Template String Functions

