c++11 标准模板(STL)本地化库 - std::isgraph(std::locale) 检查字符是否被本地环境分类为图形字符

本地化库

本地环境设施包含字符分类和字符串校对、数值、货币及日期/时间格式化和分析,以及消息取得的国际化支持。本地环境设置控制流 I/O 、正则表达式库和 C++ 标准库的其他组件的行为。

检查字符是否被本地环境分类为图形字符

std::isgraph(std::locale)

template< class charT >
bool isgraph( charT ch, const locale& loc );

检查给定字符是否为给定 locale 的 std::ctype 平面分类为图形字符(即可打印字符,除了空格)。

参数

ch - 字符
loc - 本地环境

返回值

若字符被分类为图形字符则返回 true ,否则返回 false 。

可能的实现

template< class charT >
bool isgraph( charT ch, const std::locale& loc ) {
    return std::use_facet<std::ctype<charT>>(loc).is(std::ctype_base::graph, ch);
}

调用示例

#include <iostream>
#include <locale>

void try_with(wchar_t c, const char* locale)
{
    //检查给定字符是否为给定 locale 的 std::ctype 平面分类为图形字符(即可打印字符,除了空格)。
    std::cout << "isgraph('"
              << c
              << "', locale(\""
              << locale
              << "\")) returned "
              << std::boolalpha
              << std::isgraph(c, std::locale(locale))
              << std::endl;
}

int main()
{
    //空格 (0x20, ' ')
    //换页(0x0c, '\f')
    //换行(0x0a, '\n')
    //回车(0x0d, '\r')
    //水平制表符(0x09, '\t')
    //垂直制表符(0x0b, '\v')

    std::cout << std::boolalpha;

    std::locale locale1;  // locale1 是经典 "C" 本地环境的副本
    std::cout << "locale1.name():   " << locale1.name() << std::endl;

    std::locale locale2("Chinese (Simplified)_China.936"); // locale2 为本地环境
    std::cout << "locale2.name():   " << locale2.name() << std::endl;

    const wchar_t isgraphC = L'\u042f'; // 西里尔大写字母 ya
    try_with(isgraphC, locale1.name().c_str());
    try_with(isgraphC, locale2.name().c_str());
    std::cout << std::endl;

    const wchar_t isgraphC2 = '1'; // ASCII '1'
    try_with(isgraphC2, locale1.name().c_str());
    try_with(isgraphC2, locale2.name().c_str());
    std::cout << std::endl;

    const wchar_t isgraphC3 = 1; // ASCII 1
    try_with(isgraphC3, locale1.name().c_str());
    try_with(isgraphC3, locale2.name().c_str());
    std::cout << std::endl;

    const wchar_t isgraphC4 = 'a'; // ASCII a
    try_with(isgraphC4, locale1.name().c_str());
    try_with(isgraphC4, locale2.name().c_str());
    std::cout << std::endl;

    const wchar_t isgraphC5 = ','; // ASCII ','
    try_with(isgraphC5, locale1.name().c_str());
    try_with(isgraphC5, locale2.name().c_str());
    std::cout << std::endl;

    const wchar_t isgraphC6 = ' '; // ASCII ' '
    try_with(isgraphC6, locale1.name().c_str());
    try_with(isgraphC6, locale2.name().c_str());
    std::cout << std::endl;

    const wchar_t isgraphC7 = '⨌'; //⨌
    try_with(isgraphC7, locale1.name().c_str());
    try_with(isgraphC7, locale2.name().c_str());
    std::cout << std::endl;
    return 0;
}

输出

locale1.name():   C
locale2.name():   Chinese (Simplified)_China.936
isgraph('1071', locale("C")) returned true
isgraph('1071', locale("Chinese (Simplified)_China.936")) returned true

isgraph('49', locale("C")) returned true
isgraph('49', locale("Chinese (Simplified)_China.936")) returned true

isgraph('1', locale("C")) returned false
isgraph('1', locale("Chinese (Simplified)_China.936")) returned false

isgraph('97', locale("C")) returned true
isgraph('97', locale("Chinese (Simplified)_China.936")) returned true

isgraph('44', locale("C")) returned true
isgraph('44', locale("Chinese (Simplified)_China.936")) returned true

isgraph('32', locale("C")) returned false
isgraph('32', locale("Chinese (Simplified)_China.936")) returned false

isgraph('63', locale("C")) returned true
isgraph('63', locale("Chinese (Simplified)_China.936")) returned true

相关推荐

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-03-30 08:08:04       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-03-30 08:08:04       100 阅读
  3. 在Django里面运行非项目文件

    2024-03-30 08:08:04       82 阅读
  4. Python语言-面向对象

    2024-03-30 08:08:04       91 阅读

热门阅读

  1. Tomcat

    Tomcat

    2024-03-30 08:08:04      32 阅读
  2. MacOS安装Homebrew教程

    2024-03-30 08:08:04       34 阅读
  3. 在Oracle中如何使用索引快速扫描优化全表扫描

    2024-03-30 08:08:04       45 阅读
  4. 代码随想录算法训练营 Day35 贪心算法4

    2024-03-30 08:08:04       41 阅读
  5. 中央处理器CPU

    2024-03-30 08:08:04       39 阅读
  6. 【OpenCV】Mat 构造函数

    2024-03-30 08:08:04       41 阅读
  7. Redis 过期删除策略 And 内存淘汰策略 !!!

    2024-03-30 08:08:04       39 阅读
  8. Docker从入门到放弃

    2024-03-30 08:08:04       36 阅读