Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::basic_string_view<CharT,Traits>::compare

      From cppreference.com
      <cpp‎ |string‎ |basic string view
       
       
       
      std::basic_string_view
       
      constexprint compare( basic_string_view v)constnoexcept;
      (1)(since C++17)
      constexprint compare( size_type pos1, size_type count1,
                             basic_string_view v)const;
      (2)(since C++17)
      constexprint compare( size_type pos1, size_type count1, basic_string_view v,
                             size_type pos2, size_type count2)const;
      (3)(since C++17)
      constexprint compare(const CharT* s)const;
      (4)(since C++17)
      constexprint compare( size_type pos1, size_type count1,
                             const CharT* s)const;
      (5)(since C++17)
      constexprint compare( size_type pos1, size_type count1,
                             const CharT* s, size_type count2)const;
      (6)(since C++17)

      Compares two character sequences.

      1) The lengthrlen of the sequences to compare is the smaller ofsize() andv.size(). The function compares the two views by callingtraits::compare(data(), v.data(), rlen), and returns a value according to the following table:
      ConditionResultReturn value
      Traits::compare(data(), v.data(),rlen) < 0this isless thanv<0
      Traits::compare(data(), v.data(),rlen) == 0size() < v.size()this isless thanv<0
      size() == v.size()this isequal tov0
      size() > v.size()this isgreater thanv>0
      Traits::compare(data(), v.data(),rlen) > 0this isgreater thanv>0
      2) Equivalent tosubstr(pos1, count1).compare(v).
      3) Equivalent tosubstr(pos1, count1).compare(v.substr(pos2, count2)).
      4) Equivalent tocompare(basic_string_view(s)).
      5) Equivalent tosubstr(pos1, count1).compare(basic_string_view(s)).
      6) Equivalent tosubstr(pos1, count1).compare(basic_string_view(s, count2)).

      Contents

      [edit]Parameters

      v - view to compare
      s - pointer to the character string to compare to
      count1 - number of characters of this view to compare
      pos1 - position of the first character in this view to compare
      count2 - number of characters of the given view to compare
      pos2 - position of the first character of the given view to compare

      [edit]Return value

      Negative value if this view is less than the other character sequence, zero if the both character sequences are equal, positive value if this view is greater than the other character sequence.

      [edit]Complexity

      1) Linear in the number of characters compared.

      [edit]Example

      Run this code
      #include <string_view> int main(){using std::operator""sv;    static_assert("abc"sv.compare("abcd"sv)<0);    static_assert("abcd"sv.compare("abc"sv)>0);    static_assert("abc"sv.compare("abc"sv)==0);    static_assert(""sv.compare(""sv)==0);}

      [edit]See also

      compares two strings
      (public member function ofstd::basic_string<CharT,Traits,Allocator>)[edit]
      (C++17)(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 string views
      (function template)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/string/basic_string_view/compare&oldid=152756"

      [8]ページ先頭

      ©2009-2025 Movatter.jp