std::wstring_convert<Codecvt,Elem,Wide_alloc,Byte_alloc>::converted
来自cppreference.com
| 在标头 <locale> 定义
|
||
| |
||
返回最近的 from_bytes() 或 to_bytes() 成功处理的源字符数。
返回值
示例
运行此代码
#include <codecvt>
#include <iostream>
#include <locale>
#include <string>
int main()
{
std::string utf8 = "z\u00df\u6c34\U0001d10b"; // 或 u8"zß水𝄋"
// 或 "\x7a\xc3\x9f\xe6\xb0\xb4\xf0\x9d\x84\x8b";
std::cout << "原 UTF-8 字符串的大小:" << utf8.size() << '\n';
// UTF-8 - UTF-32 标准转换刻面
std::wstring_convert<std::codecvt_utf8<char32_t>, char32_t> cvt;
// UTF-8 到 UTF-32
std::u32string utf32 = cvt.from_bytes(utf8);
std::cout << "UTF-32 字符串的大小:" << utf32.size() << '\n';
std::cout << "converted() == " << cvt.converted() << '\n';
// UTF-32 到 UTF-8
utf8 = cvt.to_bytes(utf32);
std::cout << "新 UTF-8 字符串的大小:" << utf8.size() << '\n';
std::cout << "converted() == " << cvt.converted() << '\n';
}
输出:
原 UTF-8 字符串的大小:10
UTF-32 字符串的大小:4
converted() == 10
新 UTF-8 字符串的大小:10
converted() == 4
缺陷报告
下列更改行为的缺陷报告追溯地应用于以前出版的 C++ 标准。
| 缺陷报告 | 应用于 | 出版时的行为 | 正确行为 |
|---|---|---|---|
| LWG 2174 | C++11 | 未要求 wstring_convert::converted 为 noexcept
|
已要求 |
参阅
| 转换宽字符串为字符串 (公开成员函数) | |
| 转换字节字符串为宽字符串 (公开成员函数) |