字符数字 ['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.
相关文章