Namespaces
Variants

std::collate<CharT>:: transform, do_transform

From cppreference.net
定义于头文件 <locale>
public :
string_type transform ( const CharT * low, const CharT * high ) const ;
(1)
protected :
virtual string_type do_transform ( const CharT * low, const CharT * high ) const ;
(2)
1) 公开成员函数,调用最终派生类的受保护虚成员函数 do_transform
2) 将字符序列 [ low , high ) 转换为一个字符串,该字符串与另一个字符串调用 transform() 的结果进行字典序比较(例如使用字符串的 operator< )时,产生的效果等同于对相同两个字符串调用 do_compare()

目录

参数

low - 指向待转换序列中首个字符的指针
high - 指向待转换序列结束位置后一位的指针

返回值

经过转换后的字符串,使得可以通过比较转换后字符串的字典序来替代原始字符串的排序比较。在"C"区域设置中,返回的字符串是 [ low , high ) 的精确副本。在其他区域设置中,返回字符串的内容由实现定义,且其长度可能显著增加。

注释

除了在排序中的使用外,转换字符串的实现特定格式还被 std::regex_traits<>::transform_primary 所识别,该函数能够提取等价类信息。

示例

#include <iomanip>
#include <iostream>
#include <locale>
int main()
{
    std::locale::global(std::locale("sv_SE.utf8"));
    auto& f = std::use_facet<std::collate<wchar_t>>(std::locale());
    std::wstring in1 = L"\u00e4ngel";
    std::wstring in2 = L"\u00e5r";
    std::wstring out1 = f.transform(&in1[0], &in1[0] + in1.size());
    std::wstring out2 = f.transform(&in2[0], &in2[0] + in2.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 lexicographic 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 lexicographic comparison: ängel before år

参见

转换字符串使得 strcmp 能产生与 strcoll 相同的结果
(函数)
转换宽字符串使得 wcscmp 能产生与 wcscoll 相同的结果
(函数)