java C++ - Integer.parseInt() 和 String.valueOf() 的替代方案

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27716799/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 12:15:59  来源:igfitidea点击:

C++ - Alternative to Integer.parseInt() and String.valueOf()

javac++

提问by Boris

In C++. What are the alternatives to Integer.parseInt()and String.valueOf()of Java in C++.

在 C++ 中。C++ 中 Java 的Integer.parseInt()String.valueOf()的替代方法是什么?

回答by Mohit Jain

For Integer.parseIntyou can use std::stoi, std::istringstream, sscanf, atoietc.

因为Integer.parseInt您可以使用std::stoistd::istringstreamsscanfatoi等。

For String.valueOf()alternatives, you can std::ostringstream, sprintf, std::tostringetc.

对于String.valueOf()替代方案,您可以使用std::ostringstreamsprintfstd::tostring等。

Recommendations:
c++11stoiand tostring
c++istringstreamand ostringstream
catoiand sprintf

推荐:
c++11stoiand tostring
c++istringstreamand ostringstream
catoiandsprintf

回答by erip

I would prefer to use sstream.

我更喜欢使用sstream

回答by Juned Ahsan

You may use atoi c++ function.

您可以使用 atoi c++ 函数。

int atoi (const char * str);

Convert string to integer Parses the C-string str interpreting its content as an integral number, which is returned as a value of type int.

将字符串转换为整数 解析 C 字符串 str ,将其内容解释为整数,该整数作为 int 类型的值返回。

Reference: http://www.cplusplus.com/reference/cstdlib/atoi/

参考:http: //www.cplusplus.com/reference/cstdlib/atoi/