Help me understand this line of code

if(bookAuthor[c].find(search,0) < bookAuthor[c].npos){

// displays bookTitle[c] and bookAuthor[c] 

I think "bookAuthor[c].find(search,0)" starts looking at the 0th character of the string stored in the cth index of the bookAuthor array for a string that matches the value of search, but I'm not really sure what "< bookAuthor[c].npos)" does. I looked up .npos, but it doesn't make sense to me.
1) That would be better to write as bookAuthor[c].find(search,0) != bookAuthor[c].npos
2) if substring is not found, .find() will returns npos. That condition can be read as "if search substring found"
Isn't better to just compare it to std::npos?

EDIT- I mean string::npos.

bookAuthor[c].find(search,0) != string::npos
Last edited on
std::string::npos is a static data member of class std::basic_string or simply std::string. You can specify a static data member using the class name as it is done in the beginning of this sentence or using object name. So this two statements with rare exception are identical



std:;string s;


std::string::npos is used to signal the situation "beyond the string".

So this statement

if(bookAuthor[c].find(search,0) < bookAuthor[c].npos){

means that object 'search' was found in the string that is it is not beyond the string. Usually with npos instead of object of class std::string the class name is used

if(bookAuthor[c].find(search,0) < std::string::npos){
and it would be better to write

if(bookAuthor[c].find(search,0) != std::string::npos){
Topic archived. No new replies allowed.