在线文档教程
C++
本土化 | Localizations

do_narrow

STD::Ctype::窄,做[医]窄

Defined in header
public: char narrow( CharT c, char dflt ) const;(1)
public: const CharT* narrow( const CharT* beg, const CharT* end, char dflt, char* dst ) const;(2)
protected: virtual char do_narrow( CharT c, char dflt ) const;(3)
protected: virtual const CharT* do_narrow( const CharT* beg, const CharT* end, char dflt, char* dst ) const;(4)

1,2%29公共成员函数,调用受保护的虚拟成员函数do_narrow最派生的类。

3%29转换%28可能宽的%29字符c例如,如果字符可以用单个字节%28表示,那么UTF-8编码中的ASCII字符是单字节%29。回报dflt如果不存在这种转换。

4%29对字符数组中的每个字符[beg, end),写入缩小字符%28或dflt每当将%29缩小到指向的字符数组中的连续位置时,dst...

缩窄总是成功的,并且通过调用widen()%29用于编写C++程序%29所需的基本源字符集%28 latin字母、数字和标点符号的所有字符。

如果收缩成功,则保留已知的所有字符分类类别。is()...

缩小任何数字字符可以保证如果从字符文字中减去结果'0',此差异等于原始字符的数字值。

参数

c-character to convert
dflt-default value to produce if the conversion fails
beg-pointer to the first character in an array of characters to convert
end-one past the end pointer for the array of characters to convert
dst-pointer to the first element of the array of char to fill

返回值

1,3%29缩小字符或dflt如果收缩失败

2,4%29end

二次

#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

二次

另见

wideninvokes do_widen (public member function)
narrownarrows characters (public member function of std::basic_ios)
wctobnarrows a wide character to a single-byte narrow character, if possible (function)

© cppreference.com

在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。

http://en.cppreference.com/w/cpp/locale/ctype/窄带