对于某些人来说,这可能是一个琐碎的问题,但是我找不到合适的答案。我想要的是生成一个std::string
包含所有可能的char
s的范围(比如说a ),其结果std::isalpha
为true
。
例如,对于默认语言环境,字符串应为"A...Za...z"
。但是,例如,如果语言环境为法语,则重音字母也应属于字符串。
PS:我从DieterLücking得到了一个解决方案,https: //stackoverflow.com/a/25125871/3093378它似乎可以在除我的(OS X 10.9.4
g++4.9
,clang++
LLVM版本5.1(clang-503.0.40))之外的所有平台上运行,在该平台上仅存在段错误尝试访问table[i]
该行时
if(table[i] & ctype::alpha)
我想知道是否还有其他人可以在Mac或任何其他平台上重现该错误。
您可以直接使用ctype-table来代替按字母顺序生成一组字符来对字符进行分类:
#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;
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句