[ Web Proxy ]
URL:
Viewing: http://ja.cppreference.com/cpp/string/basic_string/compare [Back]  [Original]

std::basic_string<CharT,Traits,Allocator>::compare - cppreference.com
cppreference.com

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

: cppreference.com
 
C++
(C++20)
(C++20)
(C++17)
(C++11)
(C++11)
(C++11)
 
 
std::basic_string
 
<tbody> </tbody> <tbody class="t-dcl-rev t-dcl-rev-num "> </tbody><tbody> </tbody> <tbody class="t-dcl-rev t-dcl-rev-num "> </tbody><tbody> </tbody> <tbody class="t-dcl-rev t-dcl-rev-num "> </tbody><tbody> </tbody> <tbody class="t-dcl-rev t-dcl-rev-num "> </tbody><tbody> </tbody> <tbody class="t-dcl-rev t-dcl-rev-num "> </tbody><tbody> </tbody> <tbody class="t-dcl-rev t-dcl-rev-num "> </tbody><tbody> </tbody> <tbody class="t-dcl-rev t-dcl-rev-num "> </tbody><tbody> </tbody> <tbody class="t-dcl-rev t-dcl-rev-num "> </tbody><tbody> </tbody> <tbody class="t-dcl-rev t-dcl-rev-num "> </tbody><tbody> </tbody>
(1)
int compare( const basic_string& str ) const;
(C++11)
int compare( const basic_string& str ) const noexcept;
(C++11)
(C++20)
constexpr int compare( const basic_string& str ) const noexcept;
(C++20)
(2)
int compare( size_type pos1, size_type count1, const basic_string& str ) const;
(C++20)
constexpr int compare( size_type pos1, size_type count1, const basic_string& str ) const;
(C++20)
(3)
int compare( size_type pos1, size_type count1, const basic_string& str, size_type pos2, size_type count2 ) const;
(C++14)
int compare( size_type pos1, size_type count1, const basic_string& str, size_type pos2, size_type count2 = npos ) const;
(C++14)
(C++20)
constexpr int compare( size_type pos1, size_type count1, const basic_string& str, size_type pos2, size_type count2 = npos ) const;
(C++20)
(4)
int compare( const CharT* s ) const;
(C++20)
constexpr int compare( const CharT* s ) const;
(C++20)
(5)
int compare( size_type pos1, size_type count1, const CharT* s ) const;
(C++20)
constexpr int compare( size_type pos1, size_type count1, const CharT* s ) const;
(C++20)
(6)
int compare( size_type pos1, size_type count1, const CharT* s, size_type count2 ) const;
(C++20)
constexpr int compare( size_type pos1, size_type count1, const CharT* s, size_type count2 ) const;
(C++20)
(7)
template < class T > int compare( const T& t ) const noexcept(/* see below */);
(C++17)
(C++20)
template < class T > constexpr int compare( const T& t ) const noexcept(/* see below */);
(C++20)
(8)
template < class T > int compare( size_type pos1, size_type count1, const T& t ) const;
(C++17)
(C++20)
template < class T > constexpr int compare( size_type pos1, size_type count1, const T& t ) const;
(C++20)
(9)
template < class T > int compare( size_type pos1, size_type count1, const T& t, size_type pos2, size_type count2 = npos) const;
(C++17)
(C++20)
template < class T > constexpr int compare( size_type pos1, size_type count1, const T& t, size_type pos2, size_type count2 = npos) const;
(C++20)

2

1) str
2) [pos1, pos1+count1) str count1 > size() - pos1 [pos1, size())
3) [pos1, pos1+count1) str [pos2, pos2+count2) count1 > size() - pos1 1 [pos1, size()) count2 > str.size() - pos2 2 [pos2, str.size())
4) s Traits::length(s)
5) [pos1, pos1+count1) s Traits::length(s) count1 > size() - pos1 [pos1, size())
6) [pos1, pos1+count1) [s, s + count2) If count1 > size() - pos1 the substring is [pos1, size()). (Note: the characters in the range [s, s + count2) may include null characters.)
7) std::basic_string_view<CharT, Traits> sv = t; t sv sv std::is_convertible_v<const T&, std::basic_string_view<CharT, Traits>> true std::is_convertible_v<const T&, const CharT*> false
8) std::basic_string_view<CharT, Traits> sv = t; t sv std::basic_string_view<CharT, Traits>(*this).substr(pos1, count1).compare(sv) [pos1, pos1+count1) sv std::is_convertible_v<const T&, std::basic_string_view<CharT, Traits>> true std::is_convertible_v<const T&, const CharT*> false
9) std::basic_string_view<CharT, Traits> sv = t; t sv std::basic_string_view<CharT, Traits>(*this).substr(pos1, count1).compare(sv.substr(pos2, count2)) [pos1, pos1+count1) sv [pos2, pos2+count2) std::is_convertible_v<const T&, std::basic_string_view<CharT, Traits>> true std::is_convertible_v<const T&, const CharT*> false

data1 count1 data2 count2 size_type rlen = std::min(count1, count2) Traits::compare(data1, data2, rlen) ()

Traits::compare(data1, data2, rlen) < 0 data1 data2 <0
Traits::compare(data1, data2, rlen) == 0 size1 < size2 data1 data2 <0
size1 == size2 data1 data2 0
size1 > size2 data1 data2 >0
Traits::compare(data1, data2, rlen) > 0 data1 data2 >0

str -
s -
count1 -
pos1 -
count2 -
pos2 -
t - (std::basic_string_view )

*this

*this

pos1 pos2 std::out_of_range

7)
noexcept :  
noexcept(std::is_nothrow_convertible_v<const T&, std::basic_string_view<CharT, Traits>>)
8-9) basic_string_view

C++

DR
LWG 2946 C++17 string_view overload causes ambiguity in some cases avoided by making it a template

template<class CharT, class Traits, class Alloc>
int basic_string<CharT, Traits, Alloc>::compare(const std::basic_string& s) const noexcept
{
    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)
        return 1;
    return 0;
}

std::basic_string (<, <=, ==, > )

( std::char_traits ) std::collate::compare

#include <cassert>
#include <string>
#include <iostream>
 
int main() 
{
    // 1) 
    {
        int compare_value{
            std::string{"Batman"}.compare(std::string{"Superman"})
        };
        std::cout << (
            compare_value < 0 ? "Batman comes before Superman\n" :
            compare_value > 0 ? "Superman comes before Batman\n" :
            "Superman and Batman are the same.\n"
        );
    }
    
    // 2) 
    {
        int compare_value{
            std::string{"Batman"}.compare(3, 3, std::string{"Superman"})
        };
        std::cout << (
            compare_value < 0 ? "man comes before Superman\n" :
            compare_value > 0 ? "Superman comes before man\n" :
            "man and Superman are the same.\n"
        );
    }
    
    // 3) 
    {
        std::string a{"Batman"};
        std::string b{"Superman"};
        
        int compare_value{a.compare(3, 3, b, 5, 3)};
        
        std::cout << (
            compare_value < 0 ? "man comes before man\n" :
            compare_value > 0 ? "man comes before man\n" :
            "man and man are the same.\n"
        );
        // 
        //  () 
        assert(compare_value == a.compare(3, 3, b, 5));
    }
    
    // 4) char 
    {
        int compare_value{std::string{"Batman"}.compare("Superman")};
        
        std::cout << (
            compare_value < 0 ? "Batman comes before Superman\n" :
            compare_value > 0 ? "Superman comes before Batman\n" :
            "Superman and Batman are the same.\n"
        );
    }
    
    // 5)  char 
    {
        int compare_value{std::string{"Batman"}.compare(3, 3, "Superman")};
        
        std::cout << (
            compare_value < 0 ? "man comes before Superman\n" :
            compare_value > 0 ? "Superman comes before man\n" :
            "man and Superman are the same.\n"
        );
    }
    
    // 6)  char 
    {
        int compare_value{std::string{"Batman"}.compare(0, 3, "Superman", 5)};
        
        std::cout << (
            compare_value < 0 ? "Bat comes before Super\n" :
            compare_value > 0 ? "Super comes before Bat\n" :
            "Super and Bat are the same.\n"
        );
    }
}

:

Batman comes before Superman
Superman comes before man
man and man are the same.
Batman comes before Superman
Superman comes before man
Bat comes before Super

2
() [edit]

() [edit]

() [edit]
2
() [edit]

() [edit]

Web Proxy Viewer  |  New URL  |  Original Page