C++ 本地化庫 - length



描述

它返回區間 [from, from_end) 中可以轉換為最多 max 個內部字元的外部字元數量,輸出如同應用 codecvt::in 一樣。

宣告

以下是 std::ctype::length 的宣告。

C++98

	
int length (state_type& state, const extern_type* from,
            const extern_type* from_end, size_t max) const;

C++11

int length (state_type& state, const extern_type* from,
            const extern_type* from_end, size_t max) const;

引數

  • state − 這是一個狀態物件。

  • from, from_end − 用於查詢源序列的起始和結束字元。

  • max − 用於查詢轉換後序列的最大長度。

返回值

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

異常

無異常保證 − 即使丟擲異常,也不會丟擲異常,構面對象也不會發生更改。

資料競爭

訪問構面對象。

示例

下面的示例解釋了 std::ctype::length。

#include <iostream>
#include <locale>
#include <cwchar>
#include <cstddef>

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[] = "sairamkrishna mammahe";
  
   std::mbstate_t mystate;
   const char * pc;
   wchar_t * pwc;

   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;
}

讓我們編譯並執行上面的程式,這將產生以下結果:

sairamkrishna mammahe
locale.htm
廣告
© . All rights reserved.