Jump to content
  • Advertisement
Sign in to follow this  
random_thinker

C++ : Trying to understand string.find_first_not_of() behaviour...

This topic is 4835 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have a method, as follows:
const string integerch_ = "+-0123456789";

bool isInteger(const string & strg)
{
   return (strg.find_first_not_of( integerch_ ) == string::npos);
}
The above method successfully determines whether strg is an integerch_. But when I do this:
bool isInteger(const string & strg)
{
   return (integerch_.find_first_not_of( strg ) == string::npos);
}
The above method doesn't work. I would have thought that these were interchangeable, since one string is just searching another, for both cases. The Docs are not clear as to precisely how these work, and a brief explanation of logic would be helpful. --random

Share this post


Link to post
Share on other sites
Advertisement
If you think about it in terms of sets, it becomes quite clear that the operation is not commutative. The expression
A.find_first_not_of(B) == string::npos
basically returns whether or not A is a subset of B (ignoring the fact that strings can have duplicate characters while sets can't) - and the subset operation obviously isn't commutative in the general case.

Share this post


Link to post
Share on other sites
Quote:
Original post by Sharlin
If you think about it in terms of sets, it becomes quite clear that the operation is not commutative. The expression
A.find_first_not_of(B) == string::npos
basically returns whether or not A is a subset of B (ignoring the fact that strings can have duplicate characters while sets can't) - and the subset operation obviously isn't commutative in the general case.


What an excellent, elegant explanation! You should be in the book writing business, Sharlin.

Cheers,

--random

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!