I try to find a character inside a string, but get unexpected results. I understand that it string::find(char c)returns -1when it is not found. However, I get unexpected results.
Even if the string does not include '8', it still returns true.
std::string s = "123456799";
if(s.find('8')<0)
cout << "Not Found" << endl;
else
cout << "Found" << endl;
//Output: Found
However, when used, the ==code works as expected.
std::string s = "123456799";
if(s.find('8')==-1)
cout << "Not Found" << endl;
else
cout << "Found" << endl;
//Output: Not Found
source
share