string::npos

Hi so i have homework and its to clarify what the code means and what it does.


if (numerals.find(romersk_nummer, 5) != std::string::npos)
What is your best guess as to what that line is doing?

How is numerals defined? What is contained in that variable?

How is romersk_nummer defined? What is contained in that variable?

Have you looked up any of the documentation for the classes in question?

Do you know what std::string::npos is?
I dont know what != std::string::npos is, the other part if pretty obvious i belive.


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?
npos is 'no position' and is just a constant (I think its usually -1).

No it is not usually -1 it is an unsigned type so there are no negatives. It is the largest value possible for the type.

Grime, try this:

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.
Last edited on
It is the largest value possible for the type.
That is -1 :)
I know what you mean though. But it often is defined as the int literal -1, and the compiler does the required conversion for you.

Jonnin is referring to things like this
https://github.com/gcc-mirror/gcc/blob/4bc701d37fb90229e7337bfd8f510bb74fb1aadf/libstdc%2B%2B-v3/include/std/string_view
1
2
3
4
5
  template<typename _CharT, typename _Traits = std::char_traits<_CharT>>
    class basic_string_view
    {
    public:
      static constexpr size_type npos = size_type(-1);


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.

Some discussion here: https://github.com/ericniebler/stl2/issues/182
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.

https://stackoverflow.com/questions/10168079/why-is-size-t-unsigned
Stroustrup wrote:
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.

Edit: Code example
Last edited on
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.
http://www.cplusplus.com/reference/string/string/npos/

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.
Last edited on
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.
Break that line down to it's individual pieces.

You now what an if statement is trying to do, so where are the two sides of the conditional?

Hint: numerals.find(romersk_nummer, 5) is one side, std::string::npos is the other side and the conditional is "!=".

Now what type of variable is numerals?

Next what type of variable is romersk_nummer?

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.


Topic archived. No new replies allowed.