public member function
<locale>

std::codecvt::length

int length (state_type& state, const extern_type* from,            const extern_type* from_end, size_t max) const;
返回轉換序列的長度
返回可轉換為最多 max 個內部字元的 [from,from_end) 範圍內的外部字元數,如同呼叫 codecvt::in

state 也將更新,如同為最多 max 個內部字元的緩衝區呼叫了 codecvt::in 一樣。

內部來說,此函式僅呼叫虛擬受保護成員 do_length,該成員預設按上述方式執行。

引數

state
一個 state 物件,如面例項所要求的。
通常,這是一個 mbstate_t 型別的物件,能夠跟蹤多位元組字元轉換的移位狀態。
成員型別 state_type 是面型別的狀態型別(定義為 codecvt 的第三個模板引數 stateT 的別名)。
from, from_end
指向源序列的初始和末尾字元的指標。使用的範圍是 [from,from_end),它包含 fromfrom_end 之間的所有字元,包括 from 指向的字元,但不包括 from_end 指向的字元。
請注意,空字元被視為有效字元,不會阻止函式執行直到 from_end
成員型別 extern_type 是面型別的外部字元型別(定義為 codecvt 的第二個模板引數 exnternT 的別名)。
max
轉換序列的最大長度(以內部字元為單位)。
size_t 是一個無符號整數型別。

返回值

序列字元的長度,以轉換後的內部字元為單位。

示例

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
32
// codecvt::length example
#include <iostream>       // std::wcout, std::endl
#include <locale>         // std::locale, std::codecvt, std::use_facet
#include <cwchar>         // std::mbstate_t
#include <cstddef>        // std::size_t

int main ()
{
  typedef std::codecvt<wchar_t,char,std::mbstate_t> facet_type;

  std::locale loc;
  const facet_type& myfacet = std::use_facet<facet_type>(loc);

  const char source[] = "abcdefghijklmnopqrstuvwxyz";

  // prepare objects for codecvt::length:
  std::mbstate_t mystate;
  const char * pc;
  wchar_t * pwc;

  // calculate length of dest (max 30):
  std::size_t length = myfacet.length (mystate, source, source+sizeof(source), 30);

  wchar_t* dest = new wchar_t[length];
  myfacet.in (mystate, source, source+sizeof(source), pc, dest, dest+length, pwc);

  std::wcout << dest << std::endl;

  delete[] dest;

  return 0;
}

輸出

abcdefghijklmnopqrstuvwxyz


資料競爭

將訪問面對象以及 [from,from_end) 範圍內的所有字元。
傳遞給 state 的引數將被修改。

異常安全

如果丟擲異常,面對象沒有任何變化(儘管 state 可能已更改)。

另見