Namespaces
Variants

std:: wcsxfrm

From cppreference.net
定义于头文件 <cwchar>
std:: size_t wcsxfrm ( wchar_t * dest, const wchar_t * src, std:: size_t count ) ;

src 所指向的以空字符结尾的宽字符串转换为实现定义的形式,使得使用 std::wcscmp 比较两个转换后的字符串,与在当前 C 语言环境中使用 std::wcscoll 比较原始字符串的结果相同。

转换后的字符串的前 count 个字符(包括终止空字符)将被写入目标位置,并返回完整转换后字符串的长度(不包括终止空字符)。

如果 count 0 ,则允许 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 相同的结果
(函数)
[virtual]
转换字符串以便用比较替代排序
( std::collate<CharT> 的虚受保护成员函数)
根据当前本地环境比较两个宽字符串
(函数)
C 文档 for wcsxfrm