Well did you try looking up std::string::npos? I believe that the != is also pretty obvious as well, especially since it is "pretty obvious" what find() returns.
npos is 'no position' and is just a constant (I think its usually -1).
because it isn't a pointer like strstr, its extra annoying to work with (can't treat it as a Boolean for tasks where the question is not 'where is it' but 'is it in there').
because it isn't a pointer like strstr, its extra annoying to work with (can't treat it as a Boolean for tasks where the question is not 'where is it' but 'is it in there').
How would strstr() be used differently? Both return either the position of the substring or nullptr/string::npos.. what am I missing?
string derp = "its not in there";
char * cp = strstr(derp.c_str(), "xxx");
if(cp)
cout << "found it";
strstr returns zero (aka false, aka null) when not there.
it returns a pointer to the position if found, not the index.
you have to type if(cp!= string::npos) to do the same with find, which is ok for ONE, but now consider..
cp = 0;
cp |= strstr(derp.c_str(), "xxx");
cp |= strstr(derp.c_str(), "abc");
cp |= strstr(derp.c_str(), "def");
cp |= strstr(derp.c_str(), "not");
if(cp)
cout << "found one of them";
that is where it gets a bit frustrating. Doable, but its much uglier.
and jlb, what do you get if you implicitly cast the biggest possible value for any int as signed :) Its probably 'wrongheaded' but < 0 comparisons can be easier to deal with here.
On a tangential note, there is debate over whether or not the STL was correct in designing things like size() or std::find to return unsigned values. Bjarne Stroustrup admits having positions be unsigned was a mistake.
As Stroustrup said at [42:40-45:26](https://www.youtube.com/watch?v=Puio5dly9N8#t=42m40s) and Carruth echoed: "If you need an extra bit, I am really reluctant to believe you" and "Use the next larger signed type." When we reach the point where we need more than 2^63 bytes, we're basically also at the point where we need more than 2^64 bytes, and at that point we'll be using 128-bit integers.
Using an unsigned instead of an int to gain one more bit to represent positive integers is almost never a good idea.
I'd say this doesn't apply for things like some file formats (e.g. need pixels to be 0-255), where space really matters, but otherwise applies. Languages like C# use signed lengths.
what do you get if you implicitly cast the biggest possible value for any int as signed
Since an int is usually signed you will get the largest possible value for an int, but find() returns a size_t which is an implementation defined unsigned type.
This constant is defined with a value of -1, which because size_t is an unsigned integral type, it is the largest possible representable value for this type.
it casts right back to -1 if you want. I find that useful sometimes, other times, not so much. I don't care about how many bits it has, I just find <0 a useful comparison at times.
I have to write what this code if (numerals.find(romersk_nummer, 5) != std::string::npos) does but with all of your answers its still very difficult for me to understand. i know that != means that it isnt equal to, but if i have to ask again, what really happens in just that one code. Does it reposition something, does it save it or what does it really do.
since we drug you down a long and winding road here, Ill just tell you.
it says, if the find() function returns a valid position (if the substring you were looking for was found) then it will do what is inside the if statement. If the substring was not found, it will not enter the if-statement.