C 將字串(或char)轉換為wstring(或wchar_t)
問題:
給定一個表示為陣列的字串s字元和 wstring ws,如何在不扭曲原始內容的情況下有效地將 s 的內容轉換為 ws?
答案:
為了解決這個問題,標準庫(C 11 ) 為不同字元編碼之間的轉換提供全面支持,包括UTF-8 和UTF- 16。這是使用標準函式庫的強大解決方案:
#include <locale> #include <codecvt> #include <string> std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> converter; // Convert string to wstring std::wstring ws = converter.from_bytes(s);
附加說明:
以上是如何有效率地將 C String(或 char 陣列)轉換為 wstring(或 wchar_t 陣列)?的詳細內容。更多資訊請關注PHP中文網其他相關文章!