的atoi() [英] atoi()

查看:46
本文介绍了的atoi()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

C'的atoi()的首选C ++替代品是什么?如果有的话?


-

Christopher Benson-Manica |我*应该*知道我在说什么 - 如果我

ataru(at)cyberspace.org |不,我需要知道。火焰欢迎。

What is the preferred C++ alternative to C''s atoi(), if there is one?

--
Christopher Benson-Manica | I *should* know what I''m talking about - if I
ataru(at)cyberspace.org | don''t, I need to know. Flames welcome.

推荐答案

" Christopher Benson-Manica" <在*** @ nospam.cyberspace.org>写在

消息新闻:bu ********** @ chessie.cirr.com ...
"Christopher Benson-Manica" <at***@nospam.cyberspace.org> wrote in
message news:bu**********@chessie.cirr.com...
什么是C'的首选C ++替代品atoi(),如果有
一个?
-
Christopher Benson-Manica |我*应该*知道我在说什么b $ b关于 - 如果我ataru(at)cyberspace.org |不,我需要知道。火焰欢迎。
What is the preferred C++ alternative to C''s atoi(), if there is one?
--
Christopher Benson-Manica | I *should* know what I''m talking about - if I ataru(at)cyberspace.org | don''t, I need to know. Flames welcome.




标准的替代方案是构造一个stringstream并使用>>将其

数据读入int。另见
http://www.boost.org/ libs / conversion / lexical_cast.htm


Jonathan



The standard alternative is to construct a stringstream and read its
data into an int using >>. See also
http://www.boost.org/libs/conversion/lexical_cast.htm.

Jonathan


Jonathan Turkanis< te ***** *@kangaroologic.com>这样说:
Jonathan Turkanis <te******@kangaroologic.com> spoke thus:
标准的替代方案是使用>>构造一个字符串流并将其数据读入一个int。
The standard alternative is to construct a stringstream and read its
data into an int using >>.




您会在以下情况下推荐这种方法吗?


#include< cstdlib>


int main( int argc,char * argv [])

{

int count = 0;

if(argc> 1){

count = atoi(argv [1]); //< - 值得设置一个字符串流?

}

返回EXIT_SUCCESS;

}


-

Christopher Benson-Manica |我*应该*知道我在说什么 - 如果我

ataru(at)cyberspace.org |不,我需要知道。火焰欢迎。



You would recommend such an approach in the following situation?

#include <cstdlib>

int main( int argc, char *argv[] )
{
int count=0;
if( argc > 1 ) {
count=atoi( argv[1] ); // <- worth setting up a stringstream?
}
return EXIT_SUCCESS;
}

--
Christopher Benson-Manica | I *should* know what I''m talking about - if I
ataru(at)cyberspace.org | don''t, I need to know. Flames welcome.


Christopher Benson-Manica写道:
Christopher Benson-Manica wrote:
Jonathan Turkanis< te ****** @ kangaroologic.com>这样说:

Jonathan Turkanis <te******@kangaroologic.com> spoke thus:

标准的替代方案是使用>>构建一个字符串流并将其数据读入一个int。

int main(int argc,char * argv [])
{
int count = 0;
if(argc> 1){
count = atoi(argv [1]); //< - 值得设置一个字符串流吗?
The standard alternative is to construct a stringstream and read its
data into an int using >>.

You would recommend such an approach in the following situation?

#include <cstdlib>

int main( int argc, char *argv[] )
{
int count=0;
if( argc > 1 ) {
count=atoi( argv[1] ); // <- worth setting up a stringstream?




你忘了std ::。

}
返回EXIT_SUCCESS;
}



You forgot std::.
}
return EXIT_SUCCESS;
}




好​​吧,atoi()在错误检测和&b; b $ b b恢复方面没有太大帮助。如果我正确地回忆起它,它会在溢出时默默地调用未定义的行为。我建议使用stringstream方法或

可能是strtol()或相关函数 - 无论你认为哪个是适合这种情况的



-Kevin

-

我的电子邮件地址有效,但会定期更改。

要联系我,请使用最近发布的地址。



Well, atoi() doesn''t allow much in the way of error detection &
recovery. It silently invokes undefined behavior on overflow, if I
recall correctly. I''d recommend using either the stringstream method or
possibly strtol() or a related function - whichever you think is
appropriate for the situation.

-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.


这篇关于的atoi()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆