-
April 22nd, 2009, 01:15 AM
#1
String class question
Say I have the following code:
string dog;
char cat;
cin >> cat;
if (dog.find(cat)!=string::npos)
cout << "Yup, " << cat << " is in the dog string";
I know what the code does, however, I'm a bit fuzzy on why the if statement would return either true or false. Could anyone explain the string::npos thing to a newb like me (my book is a bit vague on the subject).
Thanks
-
April 22nd, 2009, 01:32 AM
#2
Re: String class question
The various find() member functions of std::string return the index of (the first character of) the matching portion of the string, and std::string::npos if it is not found. So, if the return value is not equal to std::string::npos then there must have been a match.
-
April 22nd, 2009, 08:04 PM
#3
Re: String class question
Ah, thanks
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|