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.

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 on other sites
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 on other sites
Quote:
 Original post by SharlinIf 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

1. 1
Rutin
42
2. 2
3. 3
4. 4
5. 5

• 9
• 27
• 20
• 9
• 15
• Forum Statistics

• Total Topics
633393
• Total Posts
3011641
• Who's Online (See full list)

There are no registered users currently online

×