C++ 相当于 atoi
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7050701/
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
equivalent of atoi
提问by Mansuro
Is there a function that could replace atoi in c++. I made some research and didn't find anything to replace it, the only solutions would be using cstdlib or implementing it myself
是否有一个函数可以替换 C++ 中的 atoi。我做了一些研究并没有找到任何可以替代它的方法,唯一的解决方案是使用 cstdlib 或自己实现它
采纳答案by David Rinck
If you don't want to use Boost, C++11 added std::stoi
for strings. Similar methods exist for all types.
如果您不想使用 Boost,std::stoi
则为字符串添加了C++11 。所有类型都存在类似的方法。
std::string s = "123"
int num = std::stoi(s);
Unlike atoi
, if no conversion can be made, an invalid_argument
exception is thrown. Also, if the value is out of range for an int, an out_of_range
exception is thrown.
与 不同atoi
,如果无法进行转换,invalid_argument
则会引发异常。此外,如果该值超出 int 的范围,out_of_range
则会引发异常。
回答by Armen Tsirunyan
boost::lexical_cast
is your friend
boost::lexical_cast
是你的朋友
#include <string>
#include <boost/lexical_cast.hpp>
int main()
{
std::string s = "123";
try
{
int i = boost::lexical_cast<int>(s); //i == 123
}
catch(const boost::bad_lexical_cast&)
{
//incorrect format
}
}
回答by Armen Tsirunyan
You can use the Boost function boost::lexical_cast<> as follows:
您可以使用 Boost 函数 boost::lexical_cast<> 如下:
char* numericString = "911";
int num = boost::lexical_cast<int>( numericString );
More information can be found here(latest Boost version 1.47). Remember to handle exceptions appropriately.
可以在此处找到更多信息(最新的 Boost 版本 1.47)。记住要适当地处理异常。
回答by Torp
Without boost:stringstream ss(my_string_with_a_number); int my_res; ss >> my_res;
About as annoying as the boost version but without the added dependency. Could possibly waste more ram.
不使用 boost:stringstream ss(my_string_with_a_number); int my_res; ss >> my_res;
与 boost 版本一样烦人,但没有添加依赖项。可能会浪费更多的内存。
回答by Alastair
You don't say why atoi
is unsuitable so I am going to guess it has something to do with performance. Anyway, clarification would be helpful.
你没有说为什么atoi
不合适,所以我猜测它与性能有关。无论如何,澄清会有所帮助。
Using Boost Spirit.Qi is about an order of magnitude faster than atoi
, at least in tests done by Alex Ott.
使用 Boost Spirit.Qi 大约比Alex Ottatoi
进行的测试快一个数量级。
I don't have a reference but the last time I tested it, Boost lexical_cast
was about an order of magnitude slower than atoi
. I think the reason is that it constructs a stringstream, which is quite expensive.
我没有参考资料,但上次我测试它时,Boostlexical_cast
比atoi
. 我认为原因是它构造了一个stringstream,这是相当昂贵的。
Update: Some more recent tests
更新:一些最近的测试
回答by Mirco De Zorzi
You can use the function stoi();
您可以使用该功能 stoi();
#include <string>
// Need to include the <string> library to use stoi
int main(){
std::string s = "10";
int n = stoi(s);
}
To actually compile this you will have to enable c++11, look up on google how to do it (on code::blocks it's: Settings -> Compiler -> "Have g++ follow C++11 ISO C++ language standard") If you compile from terminal you have to add -std=c++11
要实际编译它,您必须启用 c++11,在 google 上查找如何操作(在 code::blocks 上:设置 -> 编译器 ->“让 g++ 遵循 C++11 ISO C++ 语言标准”)如果从终端编译,则必须添加 -std=c++11
g++ -std=c++11 -o program program.cpp