boost :: lexical_cast的替代方法 [英] Alternative to boost::lexical_cast

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

问题描述

我参加了一个挑战,只是为了切入点,在我的程序中的一个地方,我需要将字符串转换为一个整数。我试过boost :: lexical_cast但不幸的是它sooo sloowwww。我想是因为它执行的所有检查。我需要的是将执行这个转换,没有任何检查(我知道,将有效的数字存储为字符串)。顺便使用字符串流的方式:

  stringstream解释器; 
interpreter<< str;
interpreter>>数;

比boost :: lexical_cast更慢。

atoi ?

解决方案

您可以使用 sscanf atoi ,因为它处理语言环境。



您一定会感兴趣阅读此 C ++将字符串转换为Int Speed 基准,其特点是一个朴素的实现,是比较不同的字符串到int实现:另一个post比较不同的字符串到int实现: C ++ String to Int


I'm taking a part in a challenge, and just to cut to the point, in one of places in my program I need to convert string to an integer. I've tried boost::lexical_cast but unfortunatelly it is sooo sloowwww. I suppose because all of the checks it performs. What I need is something that would perform this conversion without any checks (I know that there will be valid numbers stored as strings). By the way using stringstream in the naive way:

stringstream interpreter;
interpreter << str;
interpreter >> number;

is even slower than boost::lexical_cast.
Is atoi the only alternative?

解决方案

You could do it using sscanf but I suspect it's slower than atoi as it handles locales.

You'll definitely be interested in reading this C++ Convert String to Int Speed benchmark that features a naive implementation that is faster than atoi.

EDIT: Another post comparing different string to int implementations: C++ String to Int.

这篇关于boost :: lexical_cast的替代方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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