C++ 本地化庫 - narrow



描述

它用於窄字元,內部,此函式簡單地呼叫虛擬保護成員 do_narrow,它在泛型模板和 char 特化 (ctype<char>) 中預設執行上述操作。

宣告

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

C++98

	
char narrow (char_type c, char dfault) const;

C++11

char narrow (char_type c, char dfault) const;

引數

  • c − 它是 char 型別。

  • low,high − 它是指向字元序列的開始和結束的指標。

  • to − 它是指向構面字元型別元素範圍的指標。

  • dfault − 它是預設字元值。

返回值

它返回 c 的轉換結果。

異常

如果丟擲異常,構面對象不會發生更改,儘管範圍內的字元可能已被影響。

資料競爭

訪問物件和範圍 [low,high) 中的元素。

示例

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

#include <iostream>
#include <locale>
#include <string>

int main () {
   std::locale loc;
   std::wstring yourname;

   std::cout << "Please enter your a word: ";
   std::getline (std::wcin,yourname);
   std::wstring::size_type length = yourname.length();

   std::cout << "The first (narrow) character in your word is: ";
   std::cout << std::use_facet< std::ctype<wchar_t> >(loc).narrow ( yourname[0], '?' );
   std::cout << '\n';

   std::cout << "The narrow transformation of your word is: ";
   char * pc = new char [length+1];
   std::use_facet< std::ctype<wchar_t> >(loc).narrow ( yourname.c_str(), 
      yourname.c_str()+length+1, '?', pc);
   std::cout << pc << '\n';

   return 0;
}

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

Please enter your a word: sai
The first (narrow) character in your word is: s
The narrow transformation of your word is: sai
locale.htm
廣告