If () skip variable check

I have the following code:

std::vector<std::string> GetSameID(std::vector<string>& allFiles, int id) { std::vector<std::string> returnVector; for(std::vector<string>::iterator it = allFiles.begin(); it != allFiles.end(); ++it) { if(GetID(*it) == id) { int index = (*it).find("_CH2.raw"); if(index > 0) { continue; //this works } if(0 < ((*it).find("_CH2.raw"))) { continue; //this doesn't } string ext = PathFindExtension((*it).c_str()); if(ext == ".raw") { returnVector.push_back(*it); } } } return returnVector; } 

My problem is why if(0 < ((*it).find("_CH2.raw"))) doesn’t work like that? My files are called ID_0_X_0_Y_128_CH1.raw ID_0_X_0_Y_128_CH2.raw (different identifiers, X and Y, for channel 1 and channel 2 on the oscilloscope).

When I do this to the end (assign an index and then check the index), it works, I don’t understand why the short version, which is more readable, does not work.

+4
source share
1 answer

According to http://en.cppreference.com/w/cpp/string/basic_string/find , string::find() returns size_t - which is an unsigned type - so it can never be less than zero.

When it does not find anything, it returns string::npos , which is also an unsigned type, but when you insert it into an int (implicitly converting it), it becomes a negative value - this is why your first code set works.

+12
source

Source: https://habr.com/ru/post/1501572/


All Articles