I'm trying to make my program check for bad user input.
开发者_StackOverflowIn this case, I only want digits. (No alphabet, etc.)
if (someStringHere.find_first_not_of("0123456789") == string::npos)
Is it correct to say that this piece of code will give me the implementation to do so?
Based on my knowledge, this returns true
if the string is only digits, and false
if it finds something other than digits from 0-9.
Your implementation looks good to me.
The following isn't an exhaustive testcase, but it's enough to prove the point given that frankly it's obvious that your approach is good (assuming ASCII input).
<tomalak> geordi << isAllNums("12345"), isAllNums("a3234e"), isAllNums("abcdef"); bool isAllNums(const std::string& str) { return str.find_first_not_of("0123456789") == string::npos; }
<geordi> true, false, false
- http://weegen.home.xs4all.nl/eelis/geordi/
精彩评论