std::ctype<CharT>::narrow, do_narrow
来自cppreference.com
                    
                                        
                    
                    
                                                            
                    |   定义于头文件  <locale>
  | 
||
|   public: char narrow( CharT c, char dflt ) const;  | 
(1) | |
|   public: const CharT* narrow( const CharT* beg, const CharT* end,   | 
(2) | |
|   protected: virtual char do_narrow( CharT c, char dflt ) const;  | 
(3) | |
|   protected: virtual const CharT* do_narrow( const CharT* beg, const CharT* end,   | 
(4) | |
1,2) 公开成员函数,调用最终导出类的受保护虚成员函数 
do_narrow 。3) 若(可能为宽)的字符 
c 能以单字节表示(例如, UTF-8 编码中的 ASCII 字符是单字节),则转换它为为多字节表示。若这种转换不存在则返回 dflt 。4) 对于字符数组 
[beg, end) 中的而每个字符,写入窄化字符(或凡在窄化失败时写入 dflt )到 dst 所指向的字符数组中的相继位置。对于来自基本源字符集(拉丁字母、数字和要求以书写 C++ 程序的标点)的字符,窄化始终成功且始终可逆(以调用 widen() )。
若窄化成功,则它保持所有 is() 所知的字符分类类别。
窄化任何数字字符保证,若从结果减去字符字面量 '0' ,则差等于原字符的数位值。
参数
| c | - | 要转换的字符 | 
| dflt | - | 若转换失败则产生的默认值 | 
| beg | - | 指向要转换的字符数组中首字符的指针 | 
| end | - | 指向要转换的字符数组的尾后一位置的指针 | 
| dst | - | 指向要填充的字符数组首元素的指针 | 
返回值
1,3) 窄化的字符,或若窄化失败则为 
dflt2,4) 
end示例
运行此代码
#include <locale> #include <iostream> void try_narrow(const std::ctype<wchar_t>& f, wchar_t c) { char n = f.narrow(c, 0); if (n) { std::wcout << '\'' << c << "' narrowed to " << +(unsigned char)n << '\n'; } else { std::wcout << '\'' << c << "' could not be narrowed\n"; } } int main() { std::locale::global(std::locale("en_US.utf8")); std::wcout.imbue(std::locale()); std::wcout << std::hex << std::showbase << "In US English UTF-8 locale:\n"; auto& f = std::use_facet<std::ctype<wchar_t>>(std::locale()); try_narrow(f, L'A'); try_narrow(f, L'A'); try_narrow(f, L'ě'); std::locale::global(std::locale("cs_CZ.iso88592")); auto& f2 = std::use_facet<std::ctype<wchar_t>>(std::locale()); std::wcout << "In Czech ISO-8859-2 locale:\n"; try_narrow(f2, L'A'); try_narrow(f2, L'A'); try_narrow(f2, L'ě'); }
输出:
In US English UTF-8 locale: 'A' narrowed to 0x41 'A' could not be narrowed 'ě' could not be narrowed In Czech ISO-8859-2 locale: 'A' narrowed to 0x41 'A' could not be narrowed 'ě' narrowed to 0xec
参阅
  调用 do_widen (公开成员函数)  | |
|   窄化字符  ( std::basic_ios<CharT,Traits> 的公开成员函数)  | |
|   若可能,则窄化宽字符为单字节窄字符  (函数)  |