|
-
October 5th, 2009, 10:41 PM
#1
std::string all numeric
What are some ways to determine if a std::string contains all numeric data? (Alpha representations of numeric data)
Code:
std::string sPrimaryNumber = "98";
sPrimaryNumber.find_first_not_of("0123456789");
John 3:16
For God so loved the world ...
-
October 5th, 2009, 11:17 PM
#2
Re: std::string all numeric
That would do it....what else are you asking for?
There may be other calls you could make, but the basic algorithm isn't going to be any different in most cases. The complexity of that algorithm is going to be O(n), and you can't really do better than that.
-
October 5th, 2009, 11:26 PM
#3
Re: std::string all numeric
 Originally Posted by kenrus
What are some ways to determine if a std::string contains all numeric data? (Alpha representations of numeric data)
It's not that easy to say because what's numeric data is something you determine yourself.
So what do you consider to be numeric data?
-
October 6th, 2009, 02:46 PM
#4
Re: std::string all numeric
Code:
bool check_string_numeric(std::string str)
{
for(int i = 0; i < str.length(); i++)
{
if(!( (str.c_str()[i] >= '0') && (str.c_str()[i] <= '9') ) )
{
//Not numeric
return false;
}
}
//numeric
return true;
}
-
October 6th, 2009, 02:50 PM
#5
Re: std::string all numeric
Hi, folks
I'm new to this forum... sorry for barging in here, but I cannot seem to figure out how to start a thread on this forum! Can you please give me a clue!
Best regards and please forgive the intrusion ...
DKean
-
October 6th, 2009, 02:58 PM
#6
Re: std::string all numeric
 Originally Posted by salehhamadeh
Code:
bool check_string_numeric(std::string str)
{
for(int i = 0; i < str.length(); i++)
{
if(!( (str.c_str()[i] >= '0') && (str.c_str()[i] <= '9') ) )
{
//Not numeric
return false;
}
}
//numeric
return true;
}
Code:
#include <algorithm>
#include <string>
#include <cctype>
bool IsNotDigit(char c)
{
return !isdigit(c);
}
bool check_string_numeric(const std::string& str)
{
return (std::find_if(str.begin(), str.end(), IsNotDigit) == str.end());
}
Regards,
Paul McKenzie
-
October 6th, 2009, 03:10 PM
#7
Re: std::string all numeric
 Originally Posted by Paul McKenzie
Code:
#include <algorithm>
#include <string>
#include <cctype>
bool IsNotDigit(char c)
{
return !isdigit(c);
}
bool check_string_numeric(const std::string& str)
{
return (std::find_if(str.begin(), str.end(), IsNotDigit) == str.end());
}
Easier to just use isdigit directly:
Code:
bool check_string_numeric(const std::string& str)
{
return (std::find_if(str.begin(), str.end(), std::not1(std::ptr_fun(::isdigit))) == str.end());
}
If you've got access to the Boost SAL, this ugliness then becomes:
Code:
boost::all(str, boost::is_digit());
-
October 6th, 2009, 03:34 PM
#8
Re: std::string all numeric
I require an algorithm to do exactly this and the one I wrote hasn't failed me yet.
This accepts any numeric value: decimal, hex, scientific notation, and has special recognition for Infinity and -Infinity.
Code:
char NumberChars[] = {"0123456789"};
char HexChars[] = {"012345679ABCDEFabcdef"};
string LS_INFINITY = "Infinity";
string NEG_LS_INFINITY = "-Infinity";
long double Infinity = 1.0f/0.0f;
bool IsNumber(const string & str, long double & out){
if (str == LS_INFINITY){
out = Infinity;
return true;
} else if (str == NEG_LS_INFINITY){
out = -Infinity;
return true;
}
if (str[0] == '-'){
if ((str[1] == '0') && (str[2] == 'x')){
if (str.find_first_not_of(HexChars, 3) != string::npos) return false;
long long tout;
stringstream ss;
ss.str(str);
ss << setbase(16);
ss >> tout;
out = (long double) tout;
return true;
} else { //negative, non hex, keep looking for failure
size_t pp = str.find_first_not_of(NumberChars, 1);
if (pp != string::npos){
if ((str[pp] == 'e') && ((str[pp + 1] == '+') || (str[pp + 1] == '-'))){ //negative scientifi notation
if (str.find_first_not_of(NumberChars, pp + 2) != string::npos) return false;
} else if (str[pp] == '.'){
pp = str.find_first_not_of(NumberChars, pp + 1);
if ((str[pp] == 'e') && ((str[pp + 1] == '+') || (str[pp + 1] == '-'))){ //negative scientifi notation
if (str.find_first_not_of(NumberChars) != string::npos) return false;
}
} else {
return false;
}
}
}
} else if ((str[0] == '0') && (str[1] == 'x')){
if (str.find_first_not_of(HexChars, 2) != string::npos) return false;
long long tout;
stringstream ss;
ss.str(str);
ss << setbase(16);
ss >> tout;
out = (long double) tout;
return true;
} else {
size_t pp = str.find_first_not_of(NumberChars);
if (pp != string::npos){
if ((str[pp] == 'e') && ((str[pp + 1] == '+') || (str[pp + 1] == '-'))){ //negative scientifi notation
if (str.find_first_not_of(NumberChars, pp + 2) != string::npos) return false;
} else if (str[pp] == '.'){
pp = str.find_first_not_of(NumberChars, pp + 1);
if ((str[pp] == 'e') && ((str[pp + 1] == '+') || (str[pp + 1] == '-'))){ //negative scientifi notation
if (str.find_first_not_of(NumberChars) != string::npos) return false;
}
} else {
return false;
}
}
}
out = atof(str.c_str());
return true;
}
Last edited by ninja9578; October 6th, 2009 at 04:36 PM.
-
October 6th, 2009, 03:51 PM
#9
Re: std::string all numeric
 Originally Posted by ninja9578
I require an algorithm to do exactly this and the one I wrote hasn't failed me yet.
Here is a call of your function that returns an incorrect result:
Code:
long double x;
bool result = IsNumber("0xg", x);
If you had written a helper function for your otherwise repeated hexadecimal conversion code you might have better avoided this mistake.
 Originally Posted by ninja9578
This accepts any numeric value: decimal, hex, scientific notation, and has special recognition for Infinity and -Infinity.
But not octal, apparently 
Out of curiosity, but why do you write:
Code:
long double Infinity = 1.0f/0.0f;
instead of using std::numeric_limits<long double>::infinity() or some equivalent existing constant?
Oh, and of course you should declare those constants as const.
-
October 6th, 2009, 04:38 PM
#10
Re: std::string all numeric
Oops, fixed now 0xg will now fail as it should. I have no need for Octal because my scripting language doesn't support it
-
October 6th, 2009, 05:13 PM
#11
Re: std::string all numeric
Some other strings to consider:
"0X1"
"1L"
"1u"
"0x"
"+1"
Last edited by Philip Nicoletti; October 6th, 2009 at 05:48 PM.
-
October 6th, 2009, 09:14 PM
#12
Re: std::string all numeric
John 3:16
For God so loved the world ...
-
October 7th, 2009, 04:14 AM
#13
Re: std::string all numeric
 Originally Posted by Plasmator
Easier to just use isdigit directly:
Yes, I know. My goal was to really take away the hand-written loop, and using the functor was easy in terms of not having to do the std::not1 stuff.
Regards,
Paul McKenzie
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|