std::collate::compare
STD::COLATE::COLATE,STD::COLATE::DO[医]比较
Defined in header | | |
---|---|---|
public: int compare( const CharT* low1, const CharT* high1, const CharT* low2, const CharT* high2 ) const; | (1) | |
protected: virtual int do_compare( const CharT* low1, const CharT* high1, const CharT* low2, const CharT* high2 ) const; | (2) | |
1%29公共成员函数,调用受保护的虚拟成员函数do_compare
最派生的类。
2%29比较字符序列[low1, high1)
到字符序列[low2, high2)
,使用此区域设置%27s排序规则,如果第一个字符串跟随第二个字符串,则返回1;如果第一个字符串位于第二个字符串之前,则返回-1;如果两个字符串相等,则返回零。
参数
low1 | - | pointer to the first character of the first string |
---|---|---|
high1 | - | one past the end pointer for the first string |
low2 | - | pointer to the first character of the second string |
high2 | - | one past the end pointer for the second string |
返回值
1如果第一个字符串大于第二个%28,即按排序规则顺序%29跟随第二个字符串;-1如果第一个字符串小于第二个%28,则在排序规则顺序%29中,则为零。
注记
如果不需要%28进行三路比较,例如提供Compare
标准算法的参数,例如std::sort
%29,std::locale::operator()
也许更合适。
排序顺序是字典顺序:字母在国家字母中的位置等价类
%29比它的情况或变体有更高的优先级。在等价类
中,小写字符在大写等价物和特定于地区的顺序之前进行排序,可以适用于具有对话式的字符。在某些地区,一组字符比较起来是单个的。校对单位
例如,"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
二次
另见
strcoll | compares two strings in accordance to the current locale (function) |
---|---|
wcscoll | compares two wide strings in accordance to the current locale (function) |
operator() | lexicographically compares two strings using this locale's collate facet (public member function of std::locale) |
© cppreference.com
在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。