std::wstring_convert<Codecvt,Elem,Wide_alloc,Byte_alloc>::converted
來自 cppreference.com
< cpp | locale | wstring convert
定義於標頭檔案 <locale> |
||
std::size_t converted() const noexcept; |
||
返回最近一次 from_bytes() 或 to_bytes() 成功處理的源字元數。
目錄 |
[編輯] 返回值
[編輯] 示例
執行此程式碼
#include <codecvt> #include <iostream> #include <locale> #include <string> int main() { std::string utf8 = "z\u00df\u6c34\U0001d10b"; // or u8"zß水𝄋" // or "\x7a\xc3\x9f\xe6\xb0\xb4\xf0\x9d\x84\x8b"; std::cout << "original UTF-8 string size: " << utf8.size() << '\n'; // the UTF-8 - UTF-32 standard conversion facet std::wstring_convert<std::codecvt_utf8<char32_t>, char32_t> cvt; // UTF-8 to 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 to 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
[編輯] 缺陷報告
下列更改行為的缺陷報告追溯地應用於以前出版的 C++ 標準。
缺陷報告 | 應用於 | 釋出時的行為 | 正確的行為 |
---|---|---|---|
LWG 2174 | C++11 | wstring_convert::converted 不要求是 noexcept |
需要 |
[編輯] 參閱
將寬字串轉換為位元組字串 (公共成員函式) | |
將位元組字串轉換為寬字串 (公共成員函式) |