gpt4 book ai didi

c++ - 是字符数字 ['0' 。 .'9'] 需要有连续的数值吗?

转载 作者:IT老高 更新时间:2023-10-28 21:57:09 24 4
gpt4 key购买 nike

C++ 实现必须将字符 '0'-'9' 设置为具有连续的数值,即:

'0' -> 0+n
'1' -> 1+n
m -> m+n
'9' -> 9+n

isdigit([classification](22.3.3.1字符分类))*的文档中找不到,我也无法在语言环境文档中找到它(但也许我看的不够仔细)。

在 2.3 字符集中,我们发现

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

但它没有提到任何排序(但也许我没有仔细看)。


*:有趣的脚注:

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 项:

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.

这是十进制数字列表的上方:

0 1 2 3 4 5 6 7 8 9

因此,实现必须使用十进制数字具有连续表示的字符集。因此,您依赖此属性的优化是安全的;但是,您依赖其他数字的连续性(例如'a'..'z')的优化是 not 可移植的 w.r.t。符合标准(另见标题 <cctype> )。如果您这样做,请确保声明该属性。

关于c++ - 是字符数字 ['0' 。 .'9'] 需要有连续的数值吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9416926/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com