• Class / Function / Type

      std::
    • Header file

      <>
    • Other / All

    最終更新日時(UTC):
    が更新

    履歴 編集

    function
    <string>

    std::basic_string::compare

    int compare(const basic_string& str) const;          // (1) C++03
    int compare(const basic_string& str) const noexcept; // (1) C++11
    constexpr int
      compare(const basic_string& str) const noexcept;   // (1) C++20
    
    int
      compare(size_type pos1,
              size_type n1,
              const basic_string& str) const; // (2) C++03
    constexpr int
      compare(size_type pos1,
              size_type n1,
              const basic_string& str) const; // (2) C++20
    
    int
      compare(size_type pos1,
              size_type n1,
              const basic_string& str,
              size_type pos2,
              size_type n2) const;        // (3) C++03
    int
      compare(size_type pos1,
              size_type n1,
              const basic_string& str,
              size_type pos2,
              size_type n2 = npos) const; // (3) C++14
    constexpr int
      compare(size_type pos1,
              size_type n1,
              const basic_string& str,
              size_type pos2,
              size_type n2 = npos) const; // (3) C++20
    
    int compare(const charT* s) const;           // (4) C++03
    constexpr int compare(const charT* s) const; // (4) C++20
    
    int
      compare(size_type pos1,
              size_type n1,
              const charT* s) const; // (5) C++03
    constexpr int
      compare(size_type pos1,
              size_type n1,
              const charT* s) const; // (5) C++20
    
    int
      compare(size_type pos1,
              size_type n1,
              const charT* s,
              size_type n2) const; // (6) C++03
    constexpr int
      compare(size_type pos1,
              size_type n1,
              const charT* s,
              size_type n2) const; // (6) C++20
    
    // string_viewを引数に取るオーバーロード
    template<class T>
    int
      compare(const T& t) const noexcept(下記参照); // (7) C++17
    template<class T>
    constexpr int
      compare(const T& t) const noexcept(下記参照); // (7) C++20
    
    template<class T>
    int
      compare(size_type pos1,
              size_type n1,
              const T& t) const; // (8) C++17
    template<class T>
    constexpr int
      compare(size_type pos1,
              size_type n1,
              const T& t) const; // (8) C++20
    
    template<class T>
    int
      compare(size_type pos1,
              size_type n1,
              const T& t,
              size_type pos2,
              size_type n2 = npos) const; // (9) C++17
    template<class T>
    constexpr int
      compare(size_type pos1,
              size_type n1,
              const T& t,
              size_type pos2,
              size_type n2 = npos) const; // (9) C++20
    

    概要

    他の文字列との比較を行う。

    テンプレートパラメータ制約

    効果

    • (1) 自身の文字列長とパラメータstrの文字列長のうち、小さい長さをrlenとし、traits::compare(data(), str.data(), rlen)を呼び出す。
    • (7) 自身の文字列長とパラメータsvの文字列長のうち、小さい長さをrlenとし、traits::compare(data(), sv.data(), rlen)を呼び出す。

    戻り値

    • (1) 比較結果が非0を返した場合は、比較結果をそのまま返す。そうでなければ、以下の条件に従って戻り値を返す:

      条件 戻り値
      size() < str.size() 0未満の値を返す
      size() == str.size() 0を返す
      size() > str.size() 0より大きい値を返す
    • (2) basic_string(*this, pos1, n1).compare(str) と等価

    • (3) basic_string(*this, pos1, n1).compare(basic_string(str, pos2, n2)) と等価
    • (4) compare(basic_string(s)) と等価
    • (5) basic_string(*this, pos, n1).compare(basic_string(s)) と等価
    • (6) basic_string(*this, pos, n1).compare(basic_string(s, n2)) と等価
    • (7) (1)と同様の結果を返す。return basic_string_view<charT, traits>(*this).compare(t);と等価。
    • (8) basic_string_view<charT, traits>(*this).substr(pos1, n1).compare(t) と等価
    • (9) 以下と等価。
      basic_string_view<charT, traits> s = *this, sv = t;
      return s.substr(pos1, n1).compare(sv.substr(pos2, n2));
      

    例外

    • (7) : noexcept(is_nothrow_convertible_v<const T&, basic_string_view<charT, traits>>)が指定される

    #include <iostream>
    #include <string>
    
    int main()
    {
      std::string a = "abc";
      std::string b = "abc";
      std::string c = "ab";
    
      std::cout << a.compare(b) << std::endl;
      std::cout << a.compare(c) << std::endl;
    }
    

    出力例

    0
    1
    

    参照