std::wstring_convert<Codecvt,Elem,Wide_alloc,Byte_alloc>::converted

来自cppreference.com
定义于头文件 <locale>
std::size_t converted() const;
(C++14 前)
std::size_t converted() const noexcept;
(C++14 起)

返回最近的 from_bytes()to_bytes() 所处理的源字符数。

返回值

最近的转换操作所消耗的字符数。

示例

#include <iostream>
#include <string>
#include <locale>
#include <codecvt>
int main()
{
    std::string utf8 =  u8"z\u00df\u6c34\U0001d10b"; // 或 u8"zß水𝄋"
                        // 或 "\x7a\xc3\x9f\xe6\xb0\xb4\xf0\x9d\x84\x8b";
    std::cout << "original UTF-8 string size: " << 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 string size: " << utf32.size() << '\n';
    std::cout << "converted() == " << cvt.converted() << '\n';
    // UTF-32 到 UTF-8
    utf8 = cvt.to_bytes(utf32);
    std::cout << "new UTF-8 string size: " << utf8.size() << '\n';
    std::cout << "converted() == " << cvt.converted() << '\n';
}

输出:

original UTF-8 string size: 10
UTF-32 string size: 4
converted() == 10
new UTF-8 string size: 10
converted() == 4

参阅

转换宽字符串为字符串
(公开成员函数)
转换字节字符串为宽字符串
(公开成员函数)