字符数字 ['0'..'9'] 是否需要具有连续的数值?
C++ 实现必须将字符“0”-“9”设置为具有连续的数值,即:
'0' -> 0+n
'1' -> 1+n
m -> m+n
'9' -> 9+n
我找不到 isdigit
的文档中提到的([分类](22.3.3.1 字符分类))*, 我也无法在语言环境文档中找到它(但也许我看起来不够努力)。
在2.3字符集中,我们发现
基本源字符集由96个字符组成:空格字符、控制字符代表 水平制表符、垂直制表符、换页符和换行符,以及以下 91 个图形字符
但它没有提到任何排序(但也许我看起来不够仔细)。
*:有趣的脚注:
当在循环中使用时,缓存 ctype<> 会更快。 facet 并直接使用它[而不是 isdigit() 等,结束注释],或者使用 ctype<>::is 的向量形式。
Must a C++ implementation set the chars '0'-'9' to have contiguous numeric values, i.e. so that:
'0' -> 0+n
'1' -> 1+n
m -> m+n
'9' -> 9+n
I cannot find it mentioned in the documentation of isdigit
([classification] (22.3.3.1 Character classification)) *,
nor can I find it in the locale documentation (but maybe I did not look hard enough).
In 2.3 Character sets, we find that
The basic source character set consists of 96 characters: the space character, the control characters representing
horizontal tab, vertical tab, form feed, and new-line, plus the following 91 graphical characters
But it doesn't mention any ordering (but maybe I did not look hard enough).
*: Interesting footnote there:
When used in a loop, it is faster to cache the ctype<> facet and use it directly [instead of isdigit() et al, end comment], or use the vector form of ctype<>::is.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
确实看起来不够仔细:在 2.3 中。字符集,第 3 项:
这是上面的十进制数字列表:
因此,实现必须使用十进制数字具有连续表示形式的字符集。因此,依赖此属性的优化是安全的;但是,依赖其他数字(例如“a”..“z”)的连续性的优化无法移植到标准(另请参阅标头)。如果您这样做,请确保声明该属性。
Indeed not looked hard enough: In 2.3. Character sets, item 3:
And this is above list of decimal digits:
Therefore, an implementation must use a character set where the decimal digits have a contiguous representation. Thus, optimizations where you rely on this property are safe; however, optimizations where you rely on the coniguity of other digits (e.g. 'a'..'z') are not portable w.r.t. to the standard (see also header
<cctype>
). If you do this, make sure to assert that property.