生成std :: isalpha的值为true的范围 [英] generate range for which std::isalpha evaluates to true
问题描述
这可能是一些小问题,但我找不到合适的答案。我想要的是生成一个范围(让我们说一个 std :: string
)包含所有可能的 char
s std :: isalpha
计算为 true
。
This may be a trivial question for some, but I cannot find an appropriate answer. What I'd like is to generate a range (let's say a std::string
) that contains all possible char
s for which std::isalpha
evaluates to true
.
例如,对于默认语言环境,字符串应为A ... Za ... z
。
For example, for the default locale, the string should be "A...Za...z"
. However, if the locale is french, for example, then accented letters should also belong to the string.
PS:我有一个来自DieterLücking的解决方案,http://stackoverflow.com/a/25125871/3093378
它似乎适用于除了我的所有平台( OS X 10.9.4
g ++ 4.9
, clang ++
(clang-503.0.40)),其中尝试访问<$ p
<$ p $ <$ p $ <$ <$ p $
PS: I got a solution from Dieter Lücking, http://stackoverflow.com/a/25125871/3093378
It seems to work on all platforms except mine (OS X 10.9.4
g++4.9
, clang++
LLVM version 5.1 (clang-503.0.40) ), where it just segfaults when trying to access table[i]
in the line
if(table[i] & ctype::alpha)
我想知道是否有其他人可以在Mac或任何其他平台上重现此错误。
I wonder if anyone else can reproduce the error on a Mac or any other platform.
推荐答案
生成一组字符,按字母顺序,您可以直接使用ctype-table:
Instead of classifying characters by generating a set of characters, being alphabetical, you might utilize the ctype-table directly:
#include <iostream>
#include <locale>
int main() {
typedef std::ctype<char> ctype;
std::locale locale;
const ctype& facet = std::use_facet<ctype>(locale);
const ctype::mask* table = facet.table();
// You might skip this and work with the table, only.
std::string result;
for(unsigned i = 0; i < facet.table_size; ++i) {
if(table[i] & ctype::alpha)
result += char(i);
}
std::cout << result << '\n';
return 0;
}
这篇关于生成std :: isalpha的值为true的范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!