相当于阿托伊

发布于 2024-11-29 07:28:18 字数 76 浏览 1 评论 0原文

C++中有没有可以替代atoi的函数? 我做了一些研究,但没有找到任何可以替代它的东西,唯一的解决方案是使用 cstdlib 或自己实现它

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

时光匆匆的小流年 2024-12-06 07:28:18

如果你不想使用Boost,C++11添加了 std::stoi 对于字符串。所有类型都存在类似的方法。

std::string s = "123"
int num = std::stoi(s);

atoi 不同,如果无法进行转换,则会出现 invalid_argument 抛出异常。此外,如果该值超出 int 的范围,则会出现 out_of_range 抛出异常。

If you don't want to use Boost, C++11 added std::stoi for strings. Similar methods exist for all types.

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.

嗼ふ静 2024-12-06 07:28:18

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   
    }
}

boost::lexical_cast is your friend

#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   
    }
}
叹梦 2024-12-06 07:28:18

您可以使用Boost函数boost::lexical_cast<>如下:

char* numericString = "911";
int num = boost::lexical_cast<int>( numericString );

更多信息可以在此处找到(最新升压版本1.47)。请记住适当地处理异常。

You can use the Boost function boost::lexical_cast<> as follows:

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.

风筝在阴天搁浅。 2024-12-06 07:28:18

没有升压:
stringstream ss(my_string_with_a_number); int my_res; SS>> my_res;
与 boost 版本一样烦人,但没有添加依赖项。可能会浪费更多的内存。

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.

谈下烟灰 2024-12-06 07:28:18

您没有说为什么 atoi 不合适,所以我猜测它与性能有关。无论如何,澄清会有所帮助。

使用 Boost Spirit.Qi 比 atoi 快一个数量级,至少在 由 Alex Ott 完成的测试

我没有参考资料,但上次测试时,Boost lexical_castatoi 慢了大约一个数量级。我认为原因是它构造了一个字符串流,这是相当昂贵的。

更新一些最近的测试

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.

Using Boost Spirit.Qi is about an order of magnitude faster than atoi, at least in tests done by Alex Ott.

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.

Update: Some more recent tests

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文