生成std::isalpha求值为true的范围
generate range for which std::isalpha evaluates to true
对于某些人来说,这可能是一个微不足道的问题,但我找不到合适的答案。我想要的是生成一个范围(假设是一个std::string
),它包含所有可能的char
s,其中std::isalpha
的计算结果为true
。
例如,对于默认的区域设置,字符串应该是"A...Za...z"
。但是,如果语言环境是法语,那么重音字母也应该属于字符串。
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;
}
如果你需要一个可移植的解决方案,那么我想不出任何理由在所有可能的char
值上运行isalpha()
不是你的最佳选择。
如果平台特定的方法是可接受的,那么您可以从平台的语言环境定义中提取信息。这里有一些文档可以帮助您开始使用POSIX平台:http://pubs.opengroup.org/onlinepubs/009696699/basedefs/xbd_chap07.html
相关文章:
- 为什么在全局范围内使用"extern int a"似乎不行?
- 尝试通过多个向量访问变量时,向量下标超出范围
- 错误:未在此范围内声明'reverse'
- 正在将指针转换为范围
- 使用std::transform将一个范围的元素添加到另一个范围中
- 无论条件是否为true,if总是在c++中执行
- 在基于范围的for循环中使用结构化绑定声明
- 如何计算数据类型的范围,例如int
- 为什么 const std::p air<K,V>& 在 std::map 上基于范围的 for 循环不起作用?
- 在C++中查找范围的长度
- 如何设置一个范围来提取我想要获得的信息
- flutter:即使shouldRepaint()返回true,自定义画家也不会重新绘制
- 并行用于C++17中数组索引范围内的循环
- 为左值和右值的包装器实现C++范围
- 求出有多少个数字是完美平方,而sqrt()是L,R范围内的素数
- 关于:C++中异常对象的范围:为什么我没有得到副本?
- 超出范围时使用对象
- 为什么所有可能的整数都"true"在 C++ 的 if 语句内的 long int 范围内,而 0 不是?
- 生成std::isalpha求值为true的范围
- 在此范围内未声明"TRUE"C++