std::collate<CharT>::transform, do_transform
来自 cppreference.cn
定义于头文件 <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 相同的结果(函数) |