std::collate<CharT>::compare, std::collate<CharT>::do_compare
提供: cppreference.com
<tbody>
</tbody>
| ヘッダ <locale> で定義
|
||
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) public メンバ関数。 最も派生したクラスの protected virtual メンバ関数
do_compare を呼びます。2) このロケールの照合ルールを用いて文字シーケンス
[low1, high1) を文字シーケンス [low2, high2) と比較し、1つめの文字列が2つめより後に来る場合は 1、1つめが2つめより前に来る場合は -1、2つの文字列が同等な場合は 0 を返します。引数
| low1 | - | 1つめの文字列の最初の文字へのポインタ |
| high1 | - | 1つめの文字列の最後の次へのポインタ |
| low2 | - | 2つめの文字列の最初の文字へのポインタ |
| high2 | - | 2つめの文字列の最後の次へのポインタ |
戻り値
1つめの文字列が2つめより大きい (つまり照合順序において2つめより後に来る) 場合は 1、1つめの文字列が2つめより小さい (照合順序において2つめより前に来る) 場合は -1、2つの文字列が同等な場合は 0。
ノート
三方比較が要求されないとき (std::sort などの標準アルゴリズムの Compare 引数を提供するときなど) は、 std::locale::operator() の方がより適切かもしれません。
照合順序は辞書順です。 アルファベットの文字 (等価クラス) の位置は大文字小文字や変種よりも高い優先度を持ちます。 等価クラス内では、小文字は同等な大文字よりも前に照合され、ダイアクリティカルマーク付きの文字にはロケール固有の順序が適用されるかもしれません。 ロケールによっては、文字のグループが単一の照合単位として比較されます。 例えば、チェコ語の "ch" は "h" より後、 "i" より前に、ハンガリー語の "dzs" は "dz" より後、 "g" より前に来ます。
例
Run this code
#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
関連項目
| 現在のロケールに従って2つの文字列を比較します (関数) | |
| 現在のロケールに従って2つのワイド文字列を比較します (関数) | |
| このロケールの照合ファセットを用いて2つの文字列を辞書的に比較します ( std::localeのパブリックメンバ関数)
|