名稱空間
變體
操作

std::wcsxfrm

來自 cppreference.com
< cpp‎ | string‎ | wide
在標頭檔案 <cwchar> 中定義
std::size_t wcsxfrm( wchar_t* dest, const wchar_t* src, std::size_t count );

src 指向的以空字元結尾的寬字串轉換為實現定義的形式,使得使用 std::wcscmp 比較兩個轉換後的字串的結果與在當前 C 區域設定中使用 std::wcscoll 比較原始字串的結果相同。

轉換後的字串的前 count 個字元(包括終止空字元)寫入目的地,並返回完整轉換後的字串的長度(不包括終止空字元)。

如果 count0,則允許 dest 為空指標。

目錄

[編輯] 注意

可以接收整個轉換後字串的緩衝區的正確長度是 1 + std::wcsxfrm(nullptr, src, 0)

當使用相同或一組寬字串進行多次依賴於區域設定的比較時,可以使用此函式,因為它更高效:只需使用 std::wcsxfrm 將所有字串轉換一次,然後使用 std::wcscmp 比較轉換後的寬字串。

[編輯] 引數

dest - 指向寬空終止字串的第一個元素的指標,用於寫入轉換後的字串
src - 指向要轉換的空終止寬字元字串的指標
count - 要輸出的最大字元數

[編輯] 返回值

轉換後的寬字串的長度,不包括終止空字元。

[編輯] 示例

#include <cwchar>
#include <iostream>
 
int main()
{
    std::setlocale(LC_ALL, "sv_SE.utf8");
 
    std::wstring in1 = L"\u00e5r";
    std::wstring out1(1 + std::wcsxfrm(nullptr, in1.c_str(), 0), L' ');
    std::wstring in2 = L"\u00e4ngel";
    std::wstring out2(1 + std::wcsxfrm(nullptr, in2.c_str(), 0), L' ');
 
    std::wcsxfrm(&out1[0], in1.c_str(), out1.size());
    std::wcsxfrm(&out2[0], in2.c_str(), out2.size());
 
    std::wcout << "In the Swedish locale: ";
    if (out1 < out2)
        std::wcout << in1 << " before " << in2 << '\n';
    else
        std::wcout << in2 << " before " << in1 << '\n';
 
    std::wcout << "In lexicographical comparison: ";
    if (in1 < in2)
        std::wcout << in1 << " before " << in2 << '\n';
    else
        std::wcout << in2 << " before " << in1 << '\n';
 
}

輸出

In the Swedish locale: år before ängel
In lexicographical comparison: ängel before år

[編輯] 另請參閱

轉換字串,使 strcmp 產生與 strcoll 相同的結果
(函式) [編輯]
[虛擬函式]
轉換字串,以便整理可以透過比較來替代
(std::collate<CharT> 的虛保護成員函式) [編輯]
根據當前語言環境比較兩個寬字串
(函式) [編輯]
C 文件 關於 wcsxfrm