strtol的C ++方式 [英] C++ way of strtol
问题描述
嗨!
在C中,我每天使用strtol看起来像使用
char * end;
strtol(text,& end,10);
读取int / long然后检查
end ==(文本+ strlen(文本));
确保转换完成(必要时)。
编写C ++时,我很高兴地收录< cstdlib>并且做了同样的事情。
使用C ++的字符串,类似
strtol(text.c_str(),& end,10);
text.size()== end - text.c_str();
可能,但对我来说很奇怪。
>
那么什么是C ++做strtol的方式?
问候,
Matthias
Matthias Kluwe写道:
嗨!
在C中,我每天使用strtol看了喜欢使用
char * end;
strtol(text,& end,10);
忽略返回值?
读取int / long然后检查
end ==(text + strlen(文本));
确保转换完成(必要时)。
编程C ++时,我很高兴地包括< cstdlib>并使用C ++的字符串,类似
strtol(text.c_str(),& end,10);
text.size() == end - text.c_str();
可能,但对我来说看起来很奇怪。
那么C ++的strtol方式是什么?
你可以使用字符串流:
long ret;
std :: istringstream stream(text) ;
if(stream>> ret&& stream.eof())
; // ok
else
; //错误
> Matthias Kluwe写道:
在C中,我每天使用strtol看起来像使用
char * end;
strtol(text,& end,10);
忽略返回值?
通常不是:-)
你可以使用字符串流:
long ret;
std :: istringstream stream(text) ;
if(stream>> ret&& stream.eof())
; //好的
其他
; //错误
这将是一个解决方案 - 谢谢!因为这不会导致复制
text根据istringstream'的构造函数声明,
这应该不会太糟糕...
问候,
Matthias
Matthias Kluwe写道:那么什么是C ++的strtol方式?
Boost有lexical_cast<>,类似这样:
int i = 999;
string s = lexical_cast< ; string>(i);
我相信它使用字符串流和运算符>>和<<在内部,
虽然如果像int< - >字符串这样的常见情况通过专业化进行了优化而不会让我感到惊讶。
--Phil。
Hi!
In C, my everyday usage of strtol looked like using
char *end;
strtol( text, &end, 10 );
to read an int/long and then checking
end == ( text + strlen( text ) );
to ensure that conversion was complete (when necessay).
When programming C++, I happily included <cstdlib> and did the same.
Using C++''s strings, something like
strtol( text.c_str(), &end, 10 );
text.size() == end - text.c_str();
is possibly, but looks strange to me.
So what''s the C++ way of doing strtol?
Regards,
Matthias
Matthias Kluwe wrote:
Hi!
In C, my everyday usage of strtol looked like using
char *end;
strtol( text, &end, 10 );
Ignoring the return value?
to read an int/long and then checking
end == ( text + strlen( text ) );
to ensure that conversion was complete (when necessay).
When programming C++, I happily included <cstdlib> and did the same.
Using C++''s strings, something like
strtol( text.c_str(), &end, 10 );
text.size() == end - text.c_str();
is possibly, but looks strange to me.
So what''s the C++ way of doing strtol?
You can use a stringstream:
long ret;
std::istringstream stream(text);
if (stream >> ret && stream.eof())
; //ok
else
; //error
>Matthias Kluwe wrote:
In C, my everyday usage of strtol looked like using char *end;
strtol( text, &end, 10 );Ignoring the return value?
Usually not :-)
You can use a stringstream: long ret;
std::istringstream stream(text);
if (stream >> ret && stream.eof())
; //ok
else
; //error
That would be a solution -- thank you!. As this does not induce copying
"text" around according the declaration of istringstream''s constructor,
this should be not too bad...
Regards,
Matthias
Matthias Kluwe wrote:So what''s the C++ way of doing strtol?
Boost has lexical_cast<>, something like this:
int i = 999;
string s = lexical_cast<string>(i);
I believe that it uses a stringstream and operator>> and << internally,
though it wouldn''t suprise me if the common cases like int<->string were
optimised though specialisation.
--Phil.
这篇关于strtol的C ++方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!