public member function
<locale>

std::codecvt::in

result in (state_type& state,   const extern_type* from, const extern_type* from_end, const extern_type*& from_next,        intern_type* to, intern_type* to_limit, intern_type*& to_next) const;
將字元轉換為內部編碼
將範圍 [from,from_end) 中的字元順序轉換為內部編碼,並將結果放入以 to 開頭的範圍中。一旦達到 to_limit,則不再嘗試儲存更多字元。

此成員函式將字元從外部型別轉換為內部型別,這通常是多位元組到寬字元的轉換(請參閱成員 in 進行反向操作)。

只要無法轉換字元,或者到達 from_end 且其字元已成功轉換,該函式就會停止轉換。

返回值以及呼叫後 from_nextto_next 的值可用於評估操作的成功和狀態。

在內部,此函式僅呼叫虛擬受保護成員 do_in,該成員預設執行上述操作。

引數

state
一個 state 物件,如面例項所要求的。
通常,這是一個 mbstate_t 型別的物件,能夠跟蹤多位元組字元轉換的移位狀態。
成員型別 state_type 是面型別的狀態型別(定義為 codecvt 的第三個模板引數 stateT 的別名)。
from, from_end
指向源序列的初始字元和結束字元的指標。使用的範圍是 [from,from_end),它包含 fromfrom_end 之間的所有字元,包括 from 指向的字元,但不包括 from_end 指向的字元。
請注意,空字元(如果有)也會被轉換,並且該函式會繼續處理它們直到 from_end
成員型別 extern_type 是 facet 的外部字元型別(定義為 codecvt 的第二個模板引數 exnternT 的別名)。
from_next
可以指向上述範圍中某個元素的指標。函式返回後,此物件將指向源範圍內最後一個成功轉換的字元之後的元素。
to, to_limit
指向目標序列的初始字元和結束字元的指標。使用的範圍最多為 [to,to_limit),它包含 toto_limit 之間的所有字元,包括 to 指向的字元,但不包括 to_limit 指向的字元。
轉換不一定會填滿目標範圍,也可能在完成之前就用盡目標範圍。
不會自動附加額外的空字元(除了從源序列轉換而來的那些)。
成員型別 intern_type 是 facet 的內部字元型別(定義為 codecvt 的第一個模板引數 internT 的別名)。
to_next
可以指向上述範圍中某個元素的指標。函式返回後,此物件將指向目標範圍內最後一個成功轉換的字元之後的元素。

返回值

值為 codecvt_base::result 型別,具有以下可能值:
成員常量intresult
ok0轉換成功:所有字元均已轉換。
partial1部分轉換:目標序列 [to,to_limit) 不夠長,或者已到達 from_end 但仍需要更多源字元才能完成目標字元的轉換。
所有之前的字元都已成功轉換。
error2轉換錯誤: from_next 指向的字元不存在有效的轉換。
所有之前的字元都已成功轉換。
noconv3無轉換:源字元型別和目標字元型別(intern_typeextern_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
32
33
34
// codecvt::in example
#include <iostream>       // std::wcout, std::wcout
#include <locale>         // std::locale, std::codecvt, std::use_facet
#include <string>         // std::wstring
#include <cwchar>         // std::mbstate_t

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

  std::locale mylocale;

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

  const char mystr[] = "Test string";

  // prepare objects to be filled by codecvt::in :
  wchar_t pwstr[sizeof(mystr)];              // the destination buffer (might be too short)
  std::mbstate_t mystate = std::mbstate_t(); // the shift state object
  const char* pc;                            // from_next
  wchar_t* pwc;                              // to_next

  // translate characters:
  facet_type::result myresult = myfacet.in (mystate,
      mystr, mystr+sizeof(mystr), pc,
      pwstr, pwstr+sizeof(mystr), pwc);

  if ( myresult == facet_type::ok )
  {
    std::wcout << L"Translation successful: ";
    std::wcout << pwstr << std::endl;
  }
  return 0;
}

輸出

Translation successful: Test string


資料競爭

將訪問面對象以及 [from,from_end) 範圍內的所有字元。
引數 statefrom_nextto_next 以及目標範圍 [to,to_limit) 中的所有字元都會被修改。

異常安全

如果丟擲異常,則facet 物件不會發生任何更改,儘管目標範圍中的字元和透過引用傳遞的引數可能會受到影響。

另見