Advertisement Jump to content
Sign in to follow this  
random_thinker

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

This topic is 4898 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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!