名稱空間
變體
操作

std::ctype<CharT>::widen, do_widen

來自 cppreference.com
< cpp‎ | 本地化‎ | ctype
 
 
 
 
 
定義於標頭檔案 <locale>
public:
CharT widen( char c ) const;
(1)
public:
const char* widen( const char* beg, const char* end, CharT* dst ) const;
(2)
protected:
virtual CharT do_widen( char c ) const;
(3)
protected:
virtual const char* do_widen( const char* beg, const char* end, CharT* dst ) const;
(4)
1,2) 公有成員函式,呼叫最派生類對應的保護虛成員函式 do_widen 的過載。過載 (1) 呼叫 do_widen(c),過載 (2) 呼叫 do_widen(beg, end, dst)
3) 使用最簡單的合理轉換將單位元組字元 c 轉換為對應的寬字元表示。通常,這僅適用於其多位元組編碼為單位元組的字元(例如 UTF-8 中的 U+0000-U+007F)。
4) 對於字元陣列 [begend) 中的每個字元,將其對應的加寬字元寫入 dst 指向的字元陣列中的連續位置。

加寬總是返回一個寬字元,但只有來自基本源字元集(直到 C++23)基本字元集(從 C++23 開始)的字元才保證具有唯一、定義明確的加寬轉換,並且保證是可逆的(透過 narrow())。實際上,所有其多位元組表示為單位元組的字元通常都會加寬為它們的寬字元對應物,而其餘可能的單位元組值通常會對映到相同的佔位符值,通常是 CharT(-1)

如果成功,加寬會保留所有 is() 已知的字元分類類別。

目錄

[編輯] 引數

c - 要轉換的字元
dflt - 如果轉換失敗,要生成的預設值
beg - 指向要轉換的字元陣列中第一個字元的指標
end - 要轉換的字元陣列的末尾指標(不包含)
dst - 指向要填充的字元陣列第一個元素的指標

[編輯] 返回值

1,3) 加寬後的字元。
2,4) end

[編輯] 示例

#include <iostream>
#include <locale>
 
void try_widen(const std::ctype<wchar_t>& f, char c)
{
    wchar_t w = f.widen(c);
    std::cout << "The single-byte character " << +(unsigned char)c
              << " widens to " << +w << '\n';
}
 
int main()
{
    std::locale::global(std::locale("cs_CZ.iso88592"));
    auto& f = std::use_facet<std::ctype<wchar_t>>(std::locale());
    std::cout << std::hex << std::showbase << "In Czech ISO-8859-2 locale:\n";
    try_widen(f, 'a');
    try_widen(f, '\xdf'); // German letter ß (U+00df) in ISO-8859-2
    try_widen(f, '\xec'); // Czech letter ě (U+011b) in ISO-8859-2
 
    std::locale::global(std::locale("cs_CZ.utf8"));
    auto& f2 = std::use_facet<std::ctype<wchar_t>>(std::locale());
    std::cout << "In Czech UTF-8 locale:\n";
    try_widen(f2, 'a');
    try_widen(f2, '\xdf'); 
    try_widen(f2, '\xec'); 
}

可能的輸出

In Czech ISO-8859-2 locale:
The single-byte character 0x61 widens to 0x61
The single-byte character 0xdf widens to 0xdf
The single-byte character 0xec widens to 0x11b
In Czech UTF-8 locale:
The single-byte character 0x61 widens to 0x61
The single-byte character 0xdf widens to 0xffffffff
The single-byte character 0xec widens to 0xffffffff

[編輯] 缺陷報告

下列更改行為的缺陷報告追溯地應用於以前出版的 C++ 標準。

缺陷報告 應用於 釋出時的行為 正確的行為
LWG 153 C++98 widen 總是呼叫過載 (4) 呼叫對應的過載

[編輯] 參閱

呼叫 do_narrow
(公有成員函式) [編輯]
寬化字元
(std::basic_ios<CharT,Traits> 的公有成員函式) [編輯]
如果可能,將單位元組窄字元加寬為寬字元
(函式) [編輯]