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

std::codecvt_byname

STD::编解码器[医]名名

Defined in header
template< class InternT, class ExternT, class State > class codecvt_byname : public std::codecvt<InternT, ExternT, State>;

std::codecvt_bynamestd::codecvtfacet,它封装在构造时指定的区域设置的多字节/宽字符转换规则。

标准库提供了四个专门化。

在标头中定义<locale>

*。

STD::编解码器[医]名称<char,char,std::mbstate[医]t>恒等转换

STD::编解码器[医]名<char16[医]T,char,std::mbstate[医]T>自C++11%29以来UTF-16和UTF-8%28之间的转换

STD::编解码器[医]名<char32[医]T,char,std::mbstate[医]T>自C++11%29以来UTF-32和UTF-8%28之间的转换

STD::编解码器[医]名<wchar[医]T,char,std::mbstate[医]T>宽字符串和窄字符集之间特定于区域设置的转换

成员函数

(constructor)constructs a new codecvt_byname facet (public member function)
(destructor)destroys a codecvt_byname facet (protected member function)

STD::编解码器[医]名称::codecvt[医]名名

explicit codecvt_byname( const char* name, std::size_t refs = 0
explicit codecvt_byname( const std::string& name, std::size_t refs = 0 (since C++11)

构造一个新的std::codecvt_byname区域设置的方面name...

refs用于资源管理:如果refs == 0时,该实现破坏了面。std::locale保存它的对象被销毁。否则,该对象不会被销毁。

参数

name-the name of the locale
refs-the number of references that link to the facet

STD::编解码器[医]名称::~codecvt[医]名名

protected: ~codecvt_byname(

摧毁了这个面。

继承自STD::编解码器

成员类型

Member typeDefinition
intern_typeinternT
extern_typeexternT
state_typestateT

成员对象

Member nameType
id (static)std::locale::id

成员函数

outinvokes do_out (public member function of std::codecvt)
ininvokes do_in (public member function of std::codecvt)
unshiftinvokes do_unshift (public member function of std::codecvt)
encodinginvokes do_encoding (public member function of std::codecvt)
always_noconvinvokes do_always_noconv (public member function of std::codecvt)
lengthinvokes do_length (public member function of std::codecvt)
max_lengthinvokes do_max_length (public member function of std::codecvt)

受保护成员函数

do_out virtualconverts a string from internT to externT, such as when writing to file (virtual protected member function of std::codecvt)
do_in virtualconverts a string from externT to internT, such as when reading from file (virtual protected member function of std::codecvt)
do_unshift virtualgenerates the termination character sequence of externT characters for incomplete conversion (virtual protected member function of std::codecvt)
do_encoding virtualreturns the number of externT characters necessary to produce one internT character, if constant (virtual protected member function of std::codecvt)
do_always_noconv virtualtests if the facet encodes an identity conversion for all valid argument values (virtual protected member function of std::codecvt)
do_length virtualcalculates the length of the externT string that would be consumed by conversion into given internT buffer (virtual protected member function of std::codecvt)
do_max_length virtualreturns the maximum number of externT characters that could be converted into a single internT character (virtual protected member function of std::codecvt)

继承自STD::编解码器[医]底座

Member typeDefinition
enum result { ok, partial, error, noconv };Unscoped enumeration type

Enumeration constantDefinition
okconversion was completed with no error
partialnot all source characters were converted
errorencountered an invalid character
noconvno conversion required, input and output types are the same

此示例演示如何使用GB 18030感知区域设置中的codecvt facet读取GB 18030编码的文件。

二次

#include <iostream> #include <fstream> #include <string> #include <locale> int main() { // GB18030 narrow multibyte encoding std::ofstream("text.txt") << "\x7a" // letter 'z', U+007a "\x81\x30\x89\x38" // letter 'ß', U+00df "\xcb\xae" // CJK ideogram '水' (water), U+6c34 "\x94\x32\xbc\x35"; // musical sign '?' (segno), U+1d10b std::wifstream fin("text.txt" fin.imbue(std::locale(fin.getloc(), new std::codecvt_byname<wchar_t, char, std::mbstate_t>("zh_CN.gb18030")) for (wchar_t c; fin.get(c ) std::cout << std::hex << std::showbase << c << '\n'; }

二次

产出:

二次

0x7a 0xdf 0x6c34 0x1d10b

二次

另见

codecvtconverts between character encodings, including UTF-8, UTF-16, UTF-32 (class template)

© cppreference.com

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

http://en.cppreference.com/w/cpp/locale/codecvt[医]名名