Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

if() skipping my variable check

Tags:

c++

I have 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 issue is, why is the if(0 < ((*it).find("_CH2.raw"))) not working that way? My files are named ID_0_X_0_Y_128_CH1.raw ID_0_X_0_Y_128_CH2.raw (different ID, X and Y, for Channel 1 and Channel 2 on the oscilloscope).

When I do it the long way around (assign index, and then check index), it works, I don't understand though why the short version, which is more readable imo, is not working.

like image 472
SinisterMJ Avatar asked Dec 15 '22 06:12

SinisterMJ


1 Answers

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

When it doesn't find something, it returns string::npos, which is also an unsigned type, but when you shove it into an int (implicitly converting it) it becomes a negative value -- this is why your first set of code works.

like image 64
Steven Avatar answered Dec 31 '22 12:12

Steven