std::collate<CharT>::compare, std::collate<CharT>::do_compare
From cppreference.com
| ヘッダー <locale> で定義 |
||
| public: int compare( const CharT* low1, const CharT* high1, |
(1) | |
| protected: virtual int do_compare( const CharT* low1, const CharT* high1, |
(2) | |
1) 公開メンバ関数。最も派生したクラスの保護仮想メンバ関数
do_compareを呼び出します。2) このロケールの照合規則を使用して、文字シーケンス
[low1, high1) を文字シーケンス[low2, high2) と比較し、最初の文字列が2番目の文字列に従う場合は1、最初の文字列が2番目の文字列より前にある場合は-1、2つの文字列が等しい場合はゼロを返します。目次 |
[編集] パラメータ
| low1 | - | 最初の文字列の最初の文字へのポインタ |
| high1 | - | 最初の文字列の終端の次の文字へのポインタ |
| low2 | - | 2番目の文字列の最初の文字へのポインタ |
| high2 | - | 2番目の文字列の終端の次の文字へのポインタ |
[編集] 戻り値
最初の文字列が2番目の文字列より大きい場合(つまり、照合順序で2番目の文字列の後に続く場合)は1、最初の文字列が2番目の文字列より小さい場合(照合順序で2番目の文字列より前に来る場合)は-1、2つの文字列が等しい場合はゼロ。
[編集] 注釈
3者比較が不要な場合(たとえば、std::sortなどの標準アルゴリズムにCompare引数を提供する場合)は、std::locale::operator()の方が適切かもしれません。
照合順序は辞書順です。つまり、アルファベットでの文字の位置(その同値クラス)が、大文字・小文字やバリアントよりも優先されます。同値クラス内では、小文字が対応する大文字よりも前に照合され、ダイアクリティカルマークを持つ文字にはロケール固有の順序が適用される場合があります。一部のロケールでは、文字のグループが単一の照合単位として比較されます。たとえば、チェコ語の"ch"は"h"の後に、"i"の前に照合されます。ハンガリー語の"dzs"は"dz"の後に、"g"の前に照合されます。
[編集] 例
このコードを実行
#include <iostream> #include <locale> #include <string> 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のpublicメンバ関数) |