Question

I was searching to validate strings in c++ and i found this mechanism. Validate in other words means, if there is variable called name, the user will not be able to input numbers to the variable only characters are allowed.

Mechanism found:

if(std::string::npos != variable.find_first_of("0123456789")

Can someone explain me the logic behind the above mechanism. I don't understand whats npos means ? i searched for an answer but couldn't find exactly what i wanted.

Thank you for your time.

Was it helpful?

Solution 2

Here are the definition found on some documentations over the net


std::string::find_first_of - find character in string. Searches the string for the first character that matches any of the characters specified in its arguments.
std::string::npos - npos is a static member constant value with the greatest possible value for an element of type size_t.


Let's use this as an example:

Line:001. std::string sVariable = "this is a test string"`
Line:002. if(std::string::npos != sVariable.find_first_of("0123456789"))`


As you can see the std::string::find_first_of tries to check if there are matches from the beginning of the string first(or index 0 of the string).

If there are no matches found, it then iterates to the next index of the string and and check matches on that index. And moves on to the next one.

At some point, the searching gets on to the end of the sVariable string, but is still not found. At that time, the find_first_of will return the std::string::npos value.

Click here for one definition of the std::string::find_first_of.


Return Value
The position of the first character that matches.
If no matches are found, the function returns string::npos.

OTHER TIPS

std::string::npos is a constant which in this context means "not found". The find_first_of function searches for the first occurrence of a character from the string passed as the argument. If not found (meaning there is no digit in variable) npos is returned.

The code uses the convention which some people find useful - instead of

if (f() != CONSTANT)

they write

if (CONSTANT != f())

It is supposedly to avoid problem when instead of != one uses = so it is assignment instead of comparison. With todays compilers and warning I find this convention not worth it, but some people opinions differ.

std::string::npos

is a constant that indicates different kinds of returned failure by std::string functions. In this case: no such character was found.

This constant is usually defined as

static const size_type npos = -1;

http://en.cppreference.com/w/cpp/string/basic_string/npos

From the documentation:

std::string::npos

npos is a static member constant value with the greatest possible value for an element of type size_t.

As a return value, it is usually used to indicate no matches.

std::string::find_first_of

If no matches are found, the function returns string::npos.

The logic of the mechanism is to test if find_first_of returns something other than npos. In this case that means the string contains at least one character from the string "0123456789".

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top