Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::collate<CharT>::compare,std::collate<CharT>::do_compare

      From cppreference.com
      <cpp‎ |locale‎ |collate
       
       
       
      Localization library
       
       
      Defined in header<locale>
      public:

      int compare(const CharT* low1,const CharT* high1,

                   const CharT* low2,const CharT* high2)const;
      (1)
      protected:

      virtualint do_compare(const CharT* low1,const CharT* high1,

                             const CharT* low2,const CharT* high2)const;
      (2)
      1) Public member function, calls the protected virtual member functiondo_compare of the most derived class.
      2) Compares the character sequence[low1high1) to the character sequence[low2high2), using this locale's collation rules, and returns1 if the first string follows the second,-1 if the first string precedes the second, zero if the two strings are equivalent.

      Contents

      [edit]Parameters

      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

      [edit]Return value

      1 if the first string is greater than the second (that is, follows the second in the collation order),-1 if the first string is less than the second (precedes the second in the collation order), zero if the two strings are equivalent.

      [edit]Notes

      When three-way comparison is not required (such as when providing aCompare argument to standard algorithms such asstd::sort),std::locale::operator() may be more appropriate.

      Collation order is the dictionary order: the position of the letter in the national alphabet (itsequivalence class) has higher priority than its case or variant. Within an equivalence class, lowercase characters collate before their uppercase equivalents and locale-specific order may apply to the characters with diacritics. In some locales, groups of characters compare as singlecollation units. For example,"ch" in Czech follows"h" and precedes"i", and"dzs" in Hungarian follows"dz" and precedes"g".

      [edit]Example

      Run this code
      #include <iostream>#include <locale>#include <string> template<typename CharT>void try_compare(conststd::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';elsestd::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");}

      Output:

      In the American locale: chrt before hrnecIn the Czech locale: hrnec before chrtIn the American locale: ängel before årIn the Swedish locale: år before ängel

      [edit]See also

      compares two strings in accordance to the current locale
      (function)[edit]
      compares two wide strings in accordance to the current locale
      (function)[edit]
      lexicographically compares two strings using this locale's collate facet
      (public member function ofstd::locale)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/locale/collate/compare&oldid=160059"

      [8]ページ先頭

      ©2009-2025 Movatter.jp