int t;
int pos = 4; //some number
string somestring(somechar);
t = somestring.find("1", pos); //whatever is in the quotations is irrelevant
Variable t should equal either npos if it is not found or the position it is found in the string. However in the script i have written t = -1. I do not know how t could be a negative number unless npos is a negative number but npos is not negative in this case. Does anyone know why this is?
Im sorry I dont under stand. Firstly npos =18446744073709551615. Secondly 2^n-1 is only 1 if n = 1 and could never equal a negative number. Also I declare npos like follows: staticconst size_t npos = -1;
I am not sure if this is correct
@helios It's all right what you said, but you stuck for an satisfying answer. Here it is:
Decalare the return value of std::string::find() always as std::string::size_type (or auto for C++11) which is the correct type for your platform. Then compare this return value with the constant std::string::npos.
1 2 3 4 5 6 7
const std::string str = "foobar";
const std::string::size_type pos = 4;
const std::string::size_type ret = str.find("1", pos);
if (ret != std::string::npos)
{
std::cout << "match at position " << ret << std::endl;
}