std::string::npos is std::size_t -1 since size_t is an unsigned type -1 is not valid
so instead it gives the largest possible value size_t can represent
I think i figured out why it still works even with -1
4294967295 is the maximum number for the unsigned int
in binary it'd be 11111111111111111111111111111111
so -1 for an unsigned data type must be the same thing as going backwards from the maximum value for the data type?
that only seems to work some of the times.
Like if i wanted to check the first 13 letters for vowels and replace this would work.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
// string::find_first_of
#include <iostream> // std::cout
#include <string> // std::string
#include <cstddef> // std::size_t
int main ()
{
std::string str ("Please, replace the vowels in this sentence by asterisks.");
std::size_t found = str.find_first_of("aeiou");
while (found!=-4294967282) //4294967295 - 13
{
str[found]='*';
found=str.find_first_of("aeiou",found+1);
}
std::cout << str << '\n';
return 0;
}
but some of the other negatives numbers wouldn't work. I have a feeling this type of behavior could be useful at some point but I think it's beyond my abilities to make it useful at the current moment.
yes -1 and 4294967295 has the same value, for unsigned int 32 bit.
std::size_t for 64 bit compiler is an unsigned int 64 bit. So -1 in unsigned int 64 bit is 2^64 - 1, way larger than 4294967295. Stick with std::string::npos. If you find that too long to type, just type str.npos (where str is a std::string)