I don’t need to know WHAT the int is, I just need to know if it is a positive, non-zero integer representation in Decimal representation, with no leading 0’s.
It’s going to be called for a large number of records, so I’d like it to be as inexpensive a check as possible.
The expected behaviour is that the system should never be passed something that doesn’t validate (because it’s normally passed ints which it converts to strings for storage) so this is just a final safety check to make sure nothing weird’s happened.
Whilst you could actually go through the process of actually converting it to an int, my assumption is that what you really want to know is whether all the characters in it are digits?
Unfortunately even then there is no option but do linearly run through the string, although this should be faster than converting to an integer first.
Using STL you can use std::find and ::isdigit and std::not1
Of course you could just write a loop
That will not totally tell you if an input string represents a positive number as they might all be zeros, and the string might be empty. We can cover that in the loop version easily.
Assumptions:
must be at least one non-zero in
there) so 0234 is a valid positive
number