Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::basic_string<CharT,Traits,Allocator>::compare

      From cppreference.com
      <cpp‎ |string‎ |basic string
       
       
       
      std::basic_string
       
      int compare(const basic_string& str)const;
      (1)(noexcept since C++11)
      (constexpr since C++20)
      int compare( size_type pos1, size_type count1,
                   const basic_string& str)const;
      (2)(constexpr since C++20)
      (3)
      int compare( size_type pos1, size_type count1,

                   const basic_string& str,

                   size_type pos2, size_type count2)const;
      (until C++14)
      int compare( size_type pos1, size_type count1,

                   const basic_string& str,

                   size_type pos2, size_type count2= npos)const;
      (since C++14)
      (constexpr since C++20)
      int compare(const CharT* s)const;
      (4)(constexpr since C++20)
      int compare( size_type pos1, size_type count1,
                   const CharT* s)const;
      (5)(constexpr since C++20)
      int compare( size_type pos1, size_type count1,
                   const CharT* s, size_type count2)const;
      (6)(constexpr since C++20)
      template<class StringViewLike>
      int compare(const StringViewLike& t)constnoexcept(/* see below */);
      (7)(since C++17)
      (constexpr since C++20)
      template<class StringViewLike>

      int compare( size_type pos1, size_type count1,

                   const StringViewLike& t)const;
      (8)(since C++17)
      (constexpr since C++20)
      template<class StringViewLike>

      int compare( size_type pos1, size_type count1,
                   const StringViewLike& t,

                   size_type pos2, size_type count2= npos)const;
      (9)(since C++17)
      (constexpr since C++20)

      Compares two character sequences.

      1) Compares this string tostr.
      2) Compares a[pos1pos1+ count1) substring of this string tostr.
      • Ifcount1> size()- pos1, the substring is[pos1size()).
      3) Compares a[pos1pos1+ count1) substring of this string to a substring[pos2pos2+ count2) ofstr.
      • Ifcount1> size()- pos1, the first substring is[pos1size()).
      • Ifcount2> str.size()- pos2, the second substring is[pos2str.size()).
      4) Compares this string to the null-terminated character sequence beginning at the character pointed to bys with lengthTraits::length(s).
      5) Compares a[pos1pos1+ count1) substring of this string to the null-terminated character sequence beginning at the character pointed to bys with lengthTraits::length(s).
      • Ifcount1> size()- pos1, the substring is[pos1size()).
      6) Compares a[pos1pos1+ count1) substring of this string to the characters in the range[ss+ count2). The characters in[ss+ count2) may include null characters.
      • Ifcount1> size()- pos1, the substring is[pos1size()).
      7-9) Implicitly convertst to a string viewsv as if bystd::basic_string_view<CharT, Traits> sv= t;, then
      7) compares this string tosv;
      8) compares a[pos1pos1+ count1) substring of this string tosv, as if bystd::basic_string_view<CharT, Traits>(*this).substr(pos1, count1).compare(sv);
      9) compares a[pos1pos1+ count1) substring of this string to a substring[pos2pos2+ count2) ofsv, as if bystd::basic_string_view<CharT, Traits>(*this)
          .substr(pos1, count1).compare(sv.substr(pos2, count2))
      .
      These overloads participate in overload resolution only ifstd::is_convertible_v<const StringViewLike&,
                           std::basic_string_view<CharT, Traits>>
      istrue andstd::is_convertible_v<const StringViewLike&,const CharT*> isfalse..

      A character sequence consisting ofcount1 characters starting atdata1 is compared to a character sequence consisting ofcount2 characters starting atdata2 as follows:

      • First, calculate the number of characters to compare, as if bysize_type rlen=std::min(count1, count2).
      • Then compare the sequences by callingTraits::compare(data1, data2, rlen). For standard strings this function performs character-by-character lexicographical comparison. If the result is zero (the character sequences are equal so far), then their sizes are compared as follows:
      ConditionResultReturn value
      Traits::compare(data1,data2,rlen) < 0data1 isless thandata2<0
      Traits::compare(data1,data2,rlen) == 0size1 <size2data1 isless thandata2<0
      size1 ==size2data1 isequal todata20
      size1 >size2data1 isgreater thandata2>0
      Traits::compare(data1,data2,rlen) > 0data1 isgreater thandata2>0

      Contents

      [edit]Parameters

      str - other string to compare to
      s - pointer to the character string to compare to
      count1 - number of characters of this string to compare
      pos1 - position of the first character in this string to compare
      count2 - number of characters of the given string to compare
      pos2 - position of the first character of the given string to compare
      t - object (convertible tostd::basic_string_view) to compare to

      [edit]Return value

      • Negative value if*this appears before the character sequence specified by the arguments, in lexicographical order.
      • Zero if both character sequences compare equivalent.
      • Positive value if*this appears after the character sequence specified by the arguments, in lexicographical order.

      [edit]Exceptions

      The overloads taking parameters namedpos1 orpos2 throwsstd::out_of_range if the argument is out of range.

      7)
      noexcept specification:  
      noexcept(std::is_nothrow_convertible_v<const T&,std::basic_string_view<CharT, Traits>>)
      8,9) Throws anything thrown by the conversion tostd::basic_string_view.

      If an exception is thrown for any reason, this function has no effect (strong exception safety guarantee).

      [edit]Possible implementation

      overload (1)
      template<class CharT,class Traits,class Alloc>intstd::basic_string<CharT, Traits, Alloc>::compare(conststd::basic_string& s)constnoexcept{    size_type lhs_sz= size();    size_type rhs_sz= s.size();int result= traits_type::compare(data(), s.data(),std::min(lhs_sz, rhs_sz));if(result!=0)return result;if(lhs_sz< rhs_sz)return-1;if(lhs_sz> rhs_sz)return1;return0;}

      [edit]Notes

      For the situations when three-way comparison is not required,std::basic_string provides the usualrelational operators (<,<=,==,>, etc).

      By default (with the defaultstd::char_traits), this function is not locale-sensitive. Seestd::collate::compare for locale-aware three-way string comparison.

      [edit]Example

      Run this code
      #include <cassert>#include <iomanip>#include <iostream>#include <string>#include <string_view> void print_compare_result(std::string_view str1,std::string_view str2,int compare_result){if(compare_result<0)std::cout<<std::quoted(str1)<<" comes before "<<std::quoted(str2)<<".\n";elseif(compare_result>0)std::cout<<std::quoted(str2)<<" comes before "<<std::quoted(str1)<<".\n";elsestd::cout<<std::quoted(str1)<<" and "<<std::quoted(str2)<<" are the same.\n";} int main(){std::string batman{"Batman"};std::string superman{"Superman"};int compare_result{0}; // 1) Compare with other string    compare_result= batman.compare(superman);std::cout<<"1) ";    print_compare_result("Batman","Superman", compare_result); // 2) Compare substring with other string    compare_result= batman.compare(3,3, superman);std::cout<<"2) ";    print_compare_result("man","Superman", compare_result); // 3) Compare substring with other substring    compare_result= batman.compare(3,3, superman,5,3);std::cout<<"3) ";    print_compare_result("man","man", compare_result); // Compare substring with other substring// defaulting to end of other stringassert(compare_result== batman.compare(3,3, superman,5)); // 4) Compare with char pointer    compare_result= batman.compare("Superman");std::cout<<"4) ";    print_compare_result("Batman","Superman", compare_result); // 5) Compare substring with char pointer    compare_result= batman.compare(3,3,"Superman");std::cout<<"5) ";    print_compare_result("man","Superman", compare_result); // 6) Compare substring with char pointer substring    compare_result= batman.compare(0,3,"Superman",5);std::cout<<"6) ";    print_compare_result("Bat","Super", compare_result);}

      Output:

      1) "Batman" comes before "Superman".2) "Superman" comes before "man".3) "man" and "man" are the same.4) "Batman" comes before "Superman".5) "Superman" comes before "man".6) "Bat" comes before "Super".

      [edit]Defect reports

      The following behavior-changing defect reports were applied retroactively to previously published C++ standards.

      DRApplied toBehavior as publishedCorrect behavior
      LWG 5C++98the parametercount2 of overload(6)
      had a default argumentnpos
      default argument removed,
      split to overloads(5) and(6)
      LWG 847C++98there was no exception safety guaranteeadded strong exception safety guarantee
      LWG 2946C++17overload(7) caused ambiguity in some casesavoided by making it a template
      P1148R0C++17noexcept for overload(7) was accidentally
      dropped by the resolution of LWG2946
      restored

      [edit]See also

      (removed in C++20)(removed in C++20)(removed in C++20)(removed in C++20)(removed in C++20)(C++20)
      lexicographically compares two strings
      (function template)[edit]
      returns a substring
      (public member function)[edit]
      defines lexicographical comparison and hashing of strings
      (class template)[edit]
      compares two strings in accordance to the current locale
      (function)[edit]
      returnstrue if one range is lexicographically less than another
      (function template)[edit]
      compares two views
      (public member function ofstd::basic_string_view<CharT,Traits>)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/string/basic_string/compare&oldid=172152"

      [8]ページ先頭

      ©2009-2025 Movatter.jp