Why does std::string.find(text,std::string:npos) not return npos?
c++, std
Solution
Looking at the spec, I think that there may be a bug in your implementation.
`basic_string::find` should return the lowest position `xpos` such that `pos <= xpos` and `xpos + str.size() <= size()` and `at(xpos + I) == str.at(I)` for all elements `I` controlled by `str`.
`basic_string::npos` is -1 converted to an unsigned type so must be the largest number representable by that unsigned type. Given that no other position `xpos` can satisfy even the first part of `npos` <= `xpos` and `find` must return `npos` on failure, as far as I can see `npos` is the only valid return value for `basic_string::find` when passed `npos` as the second parameter.
Problem
I am doing a series of searches in a string, and somewhere along the line one of the strings will be missed, and my set of searches should fail. I had expected that once the position reached std::string::npos it would stay there, but it does not. Passing std::string::npos to std::string.find seems to start the search at the beginning again ``` std::string str("frederick"); std::string::size_type pos = str.find("der",std::string::npos); TS_ASSERT_EQUALS(pos, std::string::npos); // FAIL, 3 is returned ``` Why is it not being taken to indicate the end of the string? Update: The intention is search for a series of strings in order, and check the result at the end ``` pos = str.find(string1, pos) pos = str.find(string2, pos) pos = str.find(string3, pos) if (pos != std:string::npos) { // All strings found ```