[ Web Proxy ]
URL:
Viewing: http://ru.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++11)
(C++20)
/
(C++11)
(C++11)
(C++11)
(C++17)
 
 
std::basic_string
-
(C++17)
,
/
( C++20)( C++20)( C++20)( C++20)( C++20)(C++20)
(C++11)(C++11)(C++11)
(C++11)(C++11)
(C++11)(C++11)(C++11)
(C++11)
(C++11)
 
<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 StringViewLike > int compare( const StringViewLike& t ) const noexcept(/* */);
( C++17)
( C++20)
template< class StringViewLike > constexpr int compare( const StringViewLike& t ) const noexcept(/* */);
( C++20)
(8)
template< class StringViewLike > int compare( size_type pos1, size_type count1, const StringViewLike& t ) const;
( C++17)
( C++20)
template< class StringViewLike > constexpr int compare( size_type pos1, size_type count1, const StringViewLike& t ) const;
( C++20)
(9)
template< class StringViewLike > int compare( size_type pos1, size_type count1, const StringViewLike& t, size_type pos2, size_type count2 = npos) const;
( C++17)
( C++20)
template< class StringViewLike > constexpr int compare( size_type pos1, size_type count1, const StringViewLike& t, size_type pos2, size_type count2 = npos) const;
( C++20)

.

1) str.
2) [pos1pos1 + count1) str.
  • count1 > size() - pos1, [pos1size()).
3) [pos1pos1 + count1) [pos2pos2 + count2) str.
  • count1 > size() - pos1, [pos1size()).
  • count2 > str.size() - pos2, [pos2str.size()).
4) , , , s, Traits::length(s).
5) [pos1pos1 + count1) , , , s, Traits::length(s).
  • count1 > size() - pos1, [pos1size()).
6) [pos1pos1 + count1) [ss + count2). [ss + count2) .
  • count1 > size() - pos1, [pos1size()).
7-9) t sv, std::basic_string_view<CharT, Traits> sv = t;,
7) sv;
8) [pos1pos1 + count1) sv, std::basic_string_view<CharT, Traits>(*this).substr(pos1, count1).compare(sv);
9) [pos1pos1 + count1) [pos2pos2 + count2) sv, std::basic_string_view<CharT, Traits>(*this)
.substr(pos1, count1).compare(sv.substr(pos2, count2))
.
, std::is_convertible_v<const StringViewLike&,
std::basic_string_view<CharT, Traits>>
true std::is_convertible_v<const StringViewLike&, const CharT*> false.

, count1 , data1, , count2 , data2, :

  • , 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) , std::basic_string_view.

- , ( ).

template<class CharT, class Traits, class Alloc>
int std::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 <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) << "   "
                  << std::quoted(str2) << ".\n";
    else if (compare_result > 0)
        std::cout << std::quoted(str2) << "   "
                  << std::quoted(str1) << ".\n";
    else
        std::cout << std::quoted(str1) << "  "
                  << std::quoted(str2) << " .\n";
}

int main()
{
    std::string batman{"Batman"};
    std::string superman{"Superman"};
    int compare_result{0};
    
    // 1)    
    compare_result = batman.compare(superman);
    std::cout << "1) ";
    print_compare_result("Batman", "Superman", compare_result);
    
    // 2)     
    compare_result = batman.compare(3, 3, superman);
    std::cout << "2) ";
    print_compare_result("man", "Superman", compare_result);
    
    // 3)     
    compare_result = batman.compare(3, 3, superman, 5, 3);
    std::cout << "3) ";
    print_compare_result("man", "man", compare_result);
    
    //     
    //      
    assert(compare_result == batman.compare(3, 3, superman, 5));
    
    // 4)     char
    compare_result = batman.compare("Superman");
    std::cout << "4) ";
    print_compare_result("Batman", "Superman", compare_result);
    
    // 5)      char
    compare_result = batman.compare(3, 3, "Superman");
    std::cout << "5) ";
    print_compare_result("man", "Superman", compare_result);
    
    // 6)       char
    compare_result = batman.compare(0, 3, "Superman", 5);
    std::cout << "6) ";
    print_compare_result("Bat", "Super", compare_result);
}

:

1) "Batman"   "Superman".
2) "Superman"   "man".
3) "man"  "man" .
4) "Batman"   "Superman".
5) "Superman"   "man".
6) "Bat"   "Super".

C++:

LWG 5 C++98 count2 (6)
npos
,
(5) (6)
LWG 847 C++98
LWG 2946 C++17 (7)
,
WG C++17 noexcept (7)
- LWG2946

( C++20)( C++20)( C++20)( C++20)( C++20)(C++20)

( ) []

(public -) []

( ) []

() []
true,
( ) []
(C++17)

(public - std::basic_string_view<CharT,Traits>) []

Web Proxy Viewer  |  New URL  |  Original Page