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_byname
是std::codecvt
facet,它封装在构造时指定的区域设置的多字节/宽字符转换规则。
标准库提供了四个专门化。
在标头中定义<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 type | Definition |
---|---|
intern_type | internT |
extern_type | externT |
state_type | stateT |
成员对象
Member name | Type |
---|---|
id (static) | std::locale::id |
成员函数
out | invokes do_out (public member function of std::codecvt) |
---|---|
in | invokes do_in (public member function of std::codecvt) |
unshift | invokes do_unshift (public member function of std::codecvt) |
encoding | invokes do_encoding (public member function of std::codecvt) |
always_noconv | invokes do_always_noconv (public member function of std::codecvt) |
length | invokes do_length (public member function of std::codecvt) |
max_length | invokes do_max_length (public member function of std::codecvt) |
受保护成员函数
do_out virtual | converts a string from internT to externT, such as when writing to file (virtual protected member function of std::codecvt) |
---|---|
do_in virtual | converts a string from externT to internT, such as when reading from file (virtual protected member function of std::codecvt) |
do_unshift virtual | generates the termination character sequence of externT characters for incomplete conversion (virtual protected member function of std::codecvt) |
do_encoding virtual | returns the number of externT characters necessary to produce one internT character, if constant (virtual protected member function of std::codecvt) |
do_always_noconv virtual | tests if the facet encodes an identity conversion for all valid argument values (virtual protected member function of std::codecvt) |
do_length virtual | calculates 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 virtual | returns the maximum number of externT characters that could be converted into a single internT character (virtual protected member function of std::codecvt) |
继承自STD::编解码器[医]底座
Member type | Definition |
---|---|
enum result { ok, partial, error, noconv }; | Unscoped enumeration type |
Enumeration constant | Definition |
---|---|
ok | conversion was completed with no error |
partial | not all source characters were converted |
error | encountered an invalid character |
noconv | no 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
二次
另见
codecvt | converts between character encodings, including UTF-8, UTF-16, UTF-32 (class template) |
---|
© cppreference.com
在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。