Are the character digits [#39;0#39;..#39;9#39;] required to have contiguous numeric values?(字符数字 [0..9] 是否需要具有连续的数值?)
问题描述
C++ 实现必须将字符 '0'-'9' 设置为具有连续的数值,即:
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
isdigit
([classification](22.3.3.1字符分类))*的文档中找不到,我也无法在语言环境文档中找到它(但也许我看的不够仔细).
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).
在 2.3 字符集中,我们发现
In 2.3 Character sets, we find that
基本源字符集由 96 个字符组成:空格字符、控制字符表示水平制表符、垂直制表符、换页符和换行符,以及以下 91 个图形字符
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).
*:有趣的脚注:
在循环中使用时,缓存ctype<> facet并直接使用[代替isdigit()等,结束注释],或者使用ctype<>::is的向量形式会更快.
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.
推荐答案
确实看起来不够努力:在 2.3 中.字符集,第 3 项:
Indeed not looked hard enough: In 2.3. Character sets, item 3:
在源和执行基本字符集中,中每个字符在0之后的值以上十进制数字列表应大于前一个的值.
In both the source and execution basic character sets, the value of each character after 0 in the above list of decimal digits shall be one greater than the value of the previous.
这是十进制数字列表的上方:
And this is above list of decimal digits:
0 1 2 3 4 5 6 7 8 9
因此,实现必须使用十进制数字具有连续表示的字符集.因此,您依赖此属性的优化是安全的;但是,您依赖其他数字的连续性(例如'a'..'z')的优化是 not 可移植的 w.r.t.到标准(另见标题 <cctype>
).如果您这样做,请确保声明该属性.
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.
这篇关于字符数字 ['0'..'9'] 是否需要具有连续的数值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:字符数字 ['0'..'9'] 是否需要具有连续的数值?
基础教程推荐
- 从 std::cin 读取密码 2021-01-01
- 为 C/C++ 中的项目的 makefile 生成依赖项 2022-01-01
- 管理共享内存应该分配多少内存?(助推) 2022-12-07
- 如何“在 Finder 中显示"或“在资源管理器中显 2021-01-01
- 为什么语句不能出现在命名空间范围内? 2021-01-01
- 如何在不破坏 vtbl 的情况下做相当于 memset(this, ...) 的操作? 2022-01-01
- 在 C++ 中循环遍历所有 Lua 全局变量 2021-01-01
- 如何使图像调整大小以在 Qt 中缩放? 2021-01-01
- 使用从字符串中提取的参数调用函数 2022-01-01
- Windows Media Foundation 录制音频 2021-01-01