此标头是本地化库的一部分。该头文件包括3个类,codecvt_utf8,codecvt_utf16, codecvt_utf8_utf16三个类,本篇介绍这三个类的一些用法。
成员函数
调用 do_out ( std::codecvt<InternT,ExternT,StateT> 的公开成员函数) | |
调用 do_in ( std::codecvt<InternT,ExternT,StateT> 的公开成员函数) | |
调用 do_unshift ( std::codecvt<InternT,ExternT,StateT> 的公开成员函数) | |
调用 do_encoding ( std::codecvt<InternT,ExternT,StateT> 的公开成员函数) | |
调用 do_always_noconv ( std::codecvt<InternT,ExternT,StateT> 的公开成员函数) | |
调用 do_length ( std::codecvt<InternT,ExternT,StateT> 的公开成员函数) | |
调用 do_max_length ( std::codecvt<InternT,ExternT,StateT> 的公开成员函数) |
示例代码:
#include <iostream> // std::wcout, std::wcout
#include <codecvt>
#include <fstream>
#include <locale> // std::locale, std::codecvt, std::use_facet
#include <string> // std::wstring
#include <cwchar> // std::mbstate_t
int main()
{
// codecvt::in example
typedef std::codecvt<wchar_t, char, std::mbstate_t> facet_type;
std::locale mylocale;
const facet_type& myfacet = std::use_facet<facet_type>(mylocale);
const char mystr[] = "Test string";
// prepare objects to be filled by codecvt::in :
wchar_t pwstr[sizeof(mystr)]; // the destination buffer (might be too short)
std::mbstate_t mystate = std::mbstate_t(); // the shift state object
const char* pc; // from_next
wchar_t* pwc; // to_next
// translate characters:
facet_type::result myresult = myfacet.in(mystate,
mystr, mystr + sizeof(mystr), pc,
pwstr, pwstr + sizeof(mystr), pwc);
if (myresult == facet_type::ok)
{
std::wcout << L"Translation successful: ";
std::wcout << pwstr << std::endl;
}
// codecvt::out example
typedef std::codecvt<wchar_t, char, std::mbstate_t> facet_type2;
std::locale mylocale2;
const facet_type2& myfacet2 = std::use_facet<facet_type2>(mylocale2);
std::wstring mywstring2;
std::cout << "Enter sentence: ";
std::getline(std::wcin, mywstring2);
std::wstring::size_type length2 = mywstring2.length();
// prepare objects to be filled by codecvt::out :
char* pstr2 = new char[length2 + 1]; // the destination buffer (might be too short)
std::mbstate_t mystate2 = std::mbstate_t(); // the shift state object
const wchar_t* pwc2; // from_next
char* pc2; // to_next
// call codecvt::out (translate characters):
facet_type::result myresult2 = myfacet2.out(mystate2,
mywstring2.c_str(), mywstring2.c_str() + length2 + 1, pwc2,
pstr2, pstr2 + length2 + 1, pc2);
if (myresult2 == facet_type2::ok)
std::cout << "Translation successful: " << pstr2 << '\n';
delete[] pstr2;
// codecvt::always_noconv example
std::locale loc3;
std::cout << std::boolalpha;
std::cout << "always_noconv for codecvt<char,char,mbstate_t>: ";
std::cout << std::use_facet<std::codecvt<char, char, mbstate_t> >(loc3).always_noconv();
std::cout << '\n';
std::cout << "always_noconv for codecvt<wchar_t,char,mbstate_t>: ";
std::cout << std::use_facet<std::codecvt<wchar_t, char, mbstate_t> >(loc3).always_noconv();
std::cout << '\n';
// codecvt::encoding example
std::locale loc4;
const std::codecvt<wchar_t, char, mbstate_t>& myfacet4 = std::use_facet<std::codecvt<wchar_t, char, mbstate_t> >(loc4);
std::cout << "Characteristics of codecvt<wchar_t,char,mbstate_t>:\n";
std::cout << "Encoding: " << myfacet4.encoding() << '\n';
std::cout << "Always noconv: " << myfacet4.always_noconv() << '\n';
std::cout << "Max length: " << myfacet4.max_length() << '\n';
// codecvt::length example
typedef std::codecvt<wchar_t, char, std::mbstate_t> facet_type5;
std::locale loc5;
const facet_type& myfacet5 = std::use_facet<facet_type5>(loc5);
const char source[] = "abcdefghijklmnopqrstuvwxyz";
// prepare objects for codecvt::length:
std::mbstate_t mystate5;
const char *pc5;
wchar_t *pwc5;
// calculate length of dest (max 30):
std::size_t length5 = myfacet5.length(mystate5, source, source + sizeof(source), 30);
wchar_t* dest = new wchar_t[length5];
myfacet5.in(mystate5, source, source + sizeof(source), pc5, dest, dest + length5, pwc5);
std::wcout << dest << std::endl;
delete[] dest;
// codecvt::max_length example
std::locale loc6;
std::cout << "max_length for codecvt<char,char,mbstate_t>: ";
std::cout << std::use_facet<std::codecvt<char, char, mbstate_t> >(loc6).max_length();
std::cout << '\n';
std::cout << "max_length for codecvt<wchar_t,char,mbstate_t>: ";
std::cout << std::use_facet<std::codecvt<wchar_t, char, mbstate_t> >(loc6).max_length();
std::cout << '\n';
return 0;
}
运行结果:
参考:
https://cplusplus.com/reference/codecvt/codecvt_utf8/
https://cplusplus.com/reference/codecvt/codecvt_utf16/