std::collate<CharT>::compare, std::collate<CharT>::do_compare
本地环境与平面 | |||||||||||||||||||||
本地环境 | |||||||||||||||||||||
平面类别基类 | |||||||||||||||||||||
ctype(字符类别)平面 | |||||||||||||||||||||
numeric(数值)平面 | |||||||||||||||||||||
collate(对照比较)平面 | |||||||||||||||||||||
time(时间)平面 | |||||||||||||||||||||
monetary(货币)平面 | |||||||||||||||||||||
messages(消息)平面 | |||||||||||||||||||||
字符分类与转换 | |||||||||||||||||||||
字符分类 | |||||||||||||||||||||
转换 | |||||||||||||||||||||
|
|||||||||||||||||||||
编码转换平面 | |||||||||||||||||||||
|
|
||||||||||||||||||||
C 本地环境 | |||||||||||||||||||||
成员函数 | ||||
collate::comparecollate::do_compare
|
||||
在标头
<locale>
定义
|
||
public:
int compare( const CharT* low1, const CharT* high1, |
(1) | |
protected:
virtual int
do_compare( const
CharT* low1, const
CharT* high1, |
(2) | |
do_compare
。
[low1, high1)
与字符序列
[low2, high2)
,而若第一字符串后随第二个则返回 1 ,若第一字符串前趋第二个则返回 -1 ,若二个字符串等价则返回零。参数
low1 | - | 指向第一字符串首字符的指针 |
high1 | - | 第一字符串的尾后一位置指针 |
low2 | - | 指向第二字符串首字符的指针 |
high2 | - | 第二字符串的尾后一位置指针 |
返回值
若第一字符串大于第二个(即以对照顺序后随第二个)则为 1 ,若第一字符串小于第二个(以对照顺序前趋第二个)则为 -1 ,若二个字符串等价则为零。
注意
不要求三路比较时(例如在提供 Compare
参数给如 std::sort 的标准算法时), std::locale::operator() 可能更适合。
对照顺序为字典顺序:国家字母表(其等价类)中字母的位置拥有高于其大小写或变体的优先级。在等价类内,小写字符先于其大写等价物对照,而且本地环境限定的顺序可能应用到有发音符号的字符。一些本地环境中,字符组作为单个对照单元比较。例如, "ch" 在捷克语中后随 "h" 而前趋 "i" , "dzs" 在匈牙利语中后随 "dz" 而前趋 "g" 。
示例
#include <iostream> #include <string> #include <locale> template<typename CharT> void try_compare(const std::locale& l, const CharT* p1, const CharT* p2) { auto& f = std::use_facet<std::collate<CharT>>(l); std::basic_string<CharT> s1(p1), s2(p2); if(f.compare(&s1[0], &s1[0] + s1.size(), &s2[0], &s2[0] + s2.size() ) < 0) std::wcout << p1 << " before " << p2 << '\n'; else std::wcout << p2 << " before " << p1 << '\n'; } int main() { std::locale::global(std::locale("en_US.utf8")); std::wcout.imbue(std::locale()); std::wcout << "In the American locale: "; try_compare(std::locale(), "hrnec", "chrt"); std::wcout << "In the Czech locale: "; try_compare(std::locale("cs_CZ.utf8"), "hrnec", "chrt"); std::wcout << "In the American locale: "; try_compare(std::locale(), L"år", L"ängel"); std::wcout << "In the Swedish locale: "; try_compare(std::locale("sv_SE.utf8"), L"år", L"ängel"); }
输出:
In the American locale: chrt before hrnec In the Czech locale: hrnec before chrt In the American locale: ängel before år In the Swedish locale: år before ängel
参阅
按照当前本地环境比较两个字符串 (函数) |
|
按照当前本地环境比较两个宽字符串 (函数) |
|
用此 locale 的 collate 刻面以字典序比较两个字符串 ( std::locale
的公开成员函数) |