将字符串转换为int [英] convert a string to int
问题描述
我有一个大文件,其中每行包含空格分隔的整数。任务是逐行稀疏这个文件。对于字符串到int转换我有三个解决方案:
I have a large file where each line contains space-separated integers. The task is to sparse this file line-by-line. For the string to int conversion I have three solutions:
static int stringToIntV1(const string& str) {
return (atoi(str.c_str()));
}
但是,如果我传递一个格式错误的字符串, 。例如,字符串123error转换为123.
However, if I pass a malformed string, it doesn't produce any error. For instance the string "123error" is converted to 123.
第二个解决方案:
static int stringToIntV2(const string& str)
{
int result;
istringstream myStream(str);
if (myStream >> result) {
return result;
}
// else
throw domain_error(str + " is not an int!");
}
我有同样的问题,格式错误的字符串不会引发错误。
I have the same problem here, malformed strings don't raise an error.
使用Boost的第三个解决方案(位于 http:// stackoverflow。 com / questions / 149268 / boost-library ):
Third solution with Boost (found at http://stackoverflow.com/questions/149268/boost-library):
static int stringToIntV3(const string& str)
{
int iResult = 0;
try {
iResult = lexical_cast<int>(str);
}
catch(bad_lexical_cast &) {
throw domain_error(str + " is not an int!");
}
return iResult;
}
这个结果正确。
但是,执行时间有显着差异。在大型文本文件(32 MB)上进行测试时,我获得了以下时间:
However, there is a significant difference in the execution time. Testing on a large text file (32 MB), I got the following times:
- (1) atoi:4.522s(胜出者)
- (2) with istringstream:15.303s(very slow)
- (3) with lexical_cast:10.958s(between both)
- (1) with atoi: 4.522s (winner)
- (2) with istringstream: 15.303s (very slow)
- (3) with lexical_cast: 10.958s (between the two)
我的问题:你知道如何注意atoi的格式化字符串吗?它会给最快的解决方案。或者你知道更好的解决方案吗?
My question: do you know how to notice malformed strings with atoi? It would give the fastest solution. Or do you know a better solution?
更新:感谢您的答案。根据提示,我想出了这个解决方案:
Update: Thanks for the answers. Following the tips, I came up with this solution:
static int stringToIntV4(const string& str)
{
char * pEnd;
const char * c_str = str.c_str();
int result = strtol(c_str, &pEnd, 10);
if (pEnd == c_str+str.length()) {
return result;
}
// else
throw domain_error("'" + str + "'" + " is not an int!");
}
好消息是,如果有问题,
The good news is that it yields if there is a problem and as efficient as the atoi
version.
推荐答案
我有效率高达 atoi
strtol 。它需要一个参数,它设置指向它不能转换的第一个字符,所以你可以使用它来确定整个字符串是否被转换。
I'd use strtol
. It takes a parameter that it sets to point at the first character it couldn't convert, so you can use that to determine whether the entire string was converted.
编辑:就速度而言,我预计它会比 atoi
略慢,但比你尝试的其他人快。
as far as speed goes, I'd expect it to be slightly slower than atoi
, but faster than the others you tried.
这篇关于将字符串转换为int的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!