類模板
<locale>

std::wstring_convert

template < class Codecvt,           class Elem = wchar_t,           class Wide_alloc = std::allocator<Elem>,           class Byte_alloc = std::allocator<char> > class wstring_convert;
寬字串轉換
使用型別為 Codecvt轉換物件,在寬字串位元組字串(雙向)之間執行轉換。

該物件獲得轉換物件的所有權,並負責在某個時候(當自身被銷燬時)刪除它。

模板引數

Codecvt
轉換物件的型別:這應該是一個類,具有與 codecvt 區域設定面相同的屬性,例如標頭檔案 <codecvt> 中定義的標準類之一。
Elem
寬字元型別。
這應對應於 codecvt-類轉換物件內部型別
Wide_alloc
型別為 Elem 的元素的分配器,用作寬字串型別的模板引數。
預設為:allocator<Elem>
Byte_alloc
型別為 char 的元素的分配器,用作位元組字串型別的模板引數。
預設為:allocator<char>

物件狀態

物件在內部儲存以下資料元素
型別描述
byte_string發生錯誤時返回的位元組字串
wide_string發生錯誤時返回的寬字串
Codecvt*指向轉換物件的指標
state_type轉換狀態物件,透過成員 state 訪問
size_t轉換計數,透過成員 converted 訪問

成員型別

成員型別定義說明
byte_stringbasic_string<char,char_traits<char>,Byte_alloc>窄字串型別
wide_stringbasic_string<Elem,char_traits<Elem>,Wide_alloc>寬字串型別
state_typeCodecvt::state_type
int_typechar_traits<Elem>::int_type

成員函式


轉換


觀察器


示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
// converting from UTF-32 to UTF-8
#include <iostream>       // std::cout, std::hex
#include <string>         // std::string, std::u32string
#include <locale>         // std::wstring_convert
#include <codecvt>        // std::codecvt_utf8
#include <cstdint>        // std::uint_least32_t

int main ()
{
  std::u32string str32 ( U"\U00004f60\U0000597d" );  // ni hao (你好)
  std::string str8;

  std::wstring_convert<std::codecvt_utf8<char32_t>,char32_t> cv;

  str8 = cv.to_bytes(str32);

  // print contents (as their hex representations):
  std::cout << std::hex;

  std::cout << "UTF-32: ";
  for (char32_t c : str32)
    std::cout << '[' << std::uint_least32_t(c) << ']';
  std::cout << '\n';

  std::cout << "UTF-8 : ";
  for (char c : str8)
    std::cout << '[' << int(static_cast<unsigned char>(c)) << ']';
  std::cout << '\n';

  return 0;
}

輸出

UTF-32: [4f60][597d]
UTF-8 : [e4][bd][a0][e5][a5][bd]