I just noticed this strange behavior of string::find. I have a non-empty string b and another empty string a. When I call b.find(a) it should return npos but returning 0.
#include <iostream>
#include <string>
using namespace std;
int main() {
// your code goes here
string a , b("ABC");
if ( string::npos == b.find(a) ) std::cout << std::endl << "TRUE" << std::endl;
return 0;
}
Above code doesn't print true. Can someone please explain me what this means ? Since a is empty and b is non-empty finding a empty string in non-empty doesn't make sense and hence error. So it should return npos
Thanks
findreference you will see all conditions that needs to be true for the sub-string to be found, and if you carefully check them you will see that the empty string indeed causes all to be true.b.find(a)does? It searches forainb, not the other way around....