对某些人来说,这可能是一个微不足道的问题,但我找不到合适的答案。我想要的是生成一个范围(假设一个std::string),其中包含所有可能的char,std::isalpha的计算结果为true。
例如,对于默认的区域设置,字符串应该是"A...Za...z"。但是,如果区域设置是法语,那么重音字母也应该属于字符串。
PS:我从Dieterücking,https://stackoverflow.com/a/25125871/3093378那里得到了一个解决方案,除了我的平台(OS X 10.9.4 g++4.9,clang++ llvmVersion5.1 (clang-503.0.40) ),它在尝试访问线路中的table[i]时只是分段错误。
if(table[i] & ctype::alpha)我想知道是否还有其他人可以在Mac或任何其他平台上复制错误。
发布于 2014-08-04 19:13:28
与其按字母顺序生成一组字符来对字符进行分类,您还可以直接使用ctype表:
#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;
}发布于 2014-08-04 19:19:43
如果您需要一个可移植的解决方案,那么我想不出在所有可能的char值上运行char不是最好的选择。
如果平台特定的方法是可以接受的,那么您可以从平台的区域设置定义中提取信息。以下是一些可以帮助您在POSIX平台上入门的文档:chap07.html
https://stackoverflow.com/questions/25125523
复制相似问题