Issue with string to int conversion in C++
I have an appl开发者_高级运维ication where I get a vector<string>
. I need to iterate through each element in the vector and see if a value is an integer value.
Although the vector represents strings, few of the elements can contain an integer. I need to figure out which of those elements are integers, and if an element is an integer, I need its value. If an element in the vector is a string, then I just ignore it.
I tried to use atoi(vector[index].c_str())
, but I have an issue with it. atoi
returns an integer value if the value contained in the string is an integer. If not, it returns 0
So, consider the following:
atoi("Shankar") = 0
atoi("0") = 0
and
atoi("123") = 123
atoi("123Shankar") = 123
So, how do I distinguish between the above shown cases? If this cannot be achieved using atoi, then what is the alternate solution to this problem?
Please assist.
EDIT:
I can loop through the string and see if every character is an integer, but that reduces performance, since for m strings with an average of n characters, I need to check m X n times which makes it O(n^2).
is there a better way to solve this problem?
EDIT2:
Unfortunately, I cannot use any 3rd party library for this and just use STL
EDIT3:
In my application, the vector does not contain any negative integers so I am considering Xeo's solution since sstream does not distinguish between "123" and "123Shankar"
Thanks everyone for your assistance.
Just go through your string and check every character if it's an integer. If not, break out and report false.
bool IsDigit(char c){
return '0' <= c && c <= '9';
}
bool IsInteger(std::string const& str){
size_t i = 0;
if(*str == '-') ++i;
for( ; i < str.size(); ++i){
if(!IsDigit(str[i]))
return false;
}
// all chars are integers
return true;
}
Edit
atoi
doesn't really do anything else. See this example implementation:
int StrToInt(char const* str){
int ret = 0, sign = 1;
if(*str == '-'){
sign = -1;
++str;
}
while(IsDigit(*str)){
ret *= 10; // make room for the next digit
ret += ((*str) - 0x30); // convert char to digit
++str;
}
return ret * sign;
}
You can use sscanf:
if(sscanf(s, "%d", &i) == EOF){
// error
}
or with c++:
string s = "111";
stringstream ss(s);
int i;
if((s >> i).fail()){
//error
}
Try using an istringstream
int value;
std::istringstream iss(yourvector[i]);
if(iss >> value)
std::cout<<"value is not null"<<std::endl;
std::string intStr("123");
std::string nonintStr("hello");
try {
int i = boost::lexical_cast<int>(intStr); //OK
int j = boost::lexical_cast<int>("nonintStr); //throws
}
catch (boost::bad_lexical_cast & e) {
}
The nature of the problem requires scanning each character per string -- at least until a failure occurs. There is no way to magically take the whole string at-a-glance; any character that would cause it to not be a string would have to be discovered first.
If you find a general pattern with your non-strings -- maybe they are always of the form [numbers][letters] -- then you could have a shortcut check of the LAST character of the string first to exit early. For similar functions where performance matters, I test each corner-case first, and one in the direct middle, before iterating over everything.
Here's an example:
bool IsStringValidInt( const std::string& str )
{
if( str.size() == 0 )
return false;
if( !isdigit( str[str.size() - 1] ) ||
!isdigit( str[str.size() / 2] ) )
return false;
size_t i = 0;
if( str[i] == '-' )
++i;
for( ; i < str.size(); ++i )
if( !isdigit( str[i] ) )
return false;
return true;
}
One way is to check each character of each string to see if it is a digit (usingisdigit)
The correct way to check with sscanf()
is as follows:
int val;
char dummy;
if (sscanf(str, "%d%c", &val, &dummy) == 1) {
... // "val" contains the string's integer value
} else {
... // the string does not contain an integer
}
The important thing is that the %c
component in the format string causes sscanf()
to report if there is anything after the end of the bit that can be parsed as an integer.
精彩评论