Przejdź do głównej zawartości

std::string compare() method

// (1) Const version only
constexpr int compare( const basic_string& str ) const noexcept;

// (2) Const version only
constexpr int compare( size_type pos1, size_type count1,
const basic_string& str ) const;

// (3) Const version only
constexpr int compare( size_type pos1, size_type count1,
const basic_string& str,
size_type pos2, size_type count2 = npos ) const;

// (4) Const version only
constexpr int compare( const CharT* s ) const;

// (5) Const version only
constexpr int compare( size_type pos1, size_type count1,
const CharT* s ) const;

// (6) Const version only
constexpr int compare( size_type pos1, size_type count1,
const CharT* s, size_type count2 ) const;

// (7) Const version only
template < class StringViewLike >
constexpr int compare( const StringViewLike& t ) const noexcept(/* see below */);

// (8) Const version only
template < class StringViewLike >
constexpr int compare( const StringViewLike& t ) const noexcept(/* see below */);

// (9) Const version only
template < class StringViewLike >
constexpr int compare( size_type pos1, size_type count1,
const StringViewLike& t,
size_type pos2, size_type count2 = npos) const;

Compares two character sequences.

  • (1) Compares this string to str.

  • (2) Compares a [ pos1, pos1 + count1 ) substring of this string to str.
    If count1 > size() - pos1 the substring is [ pos1, size() ).

  • (3) Compares a [pos1, pos1 + count1) substring of this string to a substring [ pos2, pos2 + count2) of str.
    If count1 > size() - pos1 the first substring is [ pos1, size() ). Likewise, count2 > str.size() - pos2 the second substring is [ pos2, str.size() ).

  • (4) Compares this string to the null-terminated character sequence beginning at the character pointed to by s with length Traits::length(s).

  • (5) Compares a [ pos1, pos1 + count1 ) substring of this string to the null-terminated character sequence beginning at the character pointed to by s with length Traits::length(s).
    If count1 > size() - pos1 the substring is [ pos1, size() ).

  • (6) Compares a [ pos1, pos1 + count1 ) substring of this string to the characters in the range [ s, s + count2 ).
    If count1 > size() - pos1 the substring is [ pos1, size() ).

    zanotuj

    The characters in the range [ s, s + count2) may include null characters.

  • (7) Implicitly converts t to a string view sv as if by std::basic_string_view<CharT, Traits> sv = t;, then compares this string to sv.

    Overload Resolution

    This overload participates in overload resolution only if std::is_convertible_v<const StringViewLike&, std::basic_string_view<CharT, Traits>> is true and std::is_convertible_v<const StringViewLike&, const CharT*> is false.

  • (8) Implicitly converts t to a string view sv as if by std::basic_string_view<CharT, Traits> sv = t;, then compares a [ pos1, pos1 + count1 ) substring of this string to sv, as if by std::basic_string_view<CharT, Traits>(*this).substr(pos1, count1).compare(sv).

    Overload Resolution

    This overload participates in overload resolution only if std::is_convertible_v<const StringViewLike&, std::basic_string_view<CharT, Traits>> is true and std::is_convertible_v<const StringViewLike&, const CharT*> is false.

  • (9) Implicitly converts t to a string view sv as if by std::basic_string_view<CharT, Traits> sv = t;, then compares a [ pos1, pos1 + count1 ) substring of this string to a substring [ pos2, pos2 + count2 ) of sv as if by std::basic_string_view<CharT, Traits>(*this).substr(pos1, count1).compare(sv.substr(pos2, count2)).

    Overload Resolution

    This overload participates in overload resolution only if std::is_convertible_v<const StringViewLike&, std::basic_string_view<CharT, Traits>> is true and std::is_convertible_v<const StringViewLike&, const CharT*> is false.

A character sequence consisting of count1 characters starting at data1 is compared to a character sequence consisting of count2 characters starting at data2 as follows:

  • (1) Calculate the number of characters to compare, as if by
    size_type rlen = std::min(count1, count2)
  • (2) Then compare the sequences as if by
    Traits::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:

important

This section requires improvement. You can help by editing this doc page.

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 to std::basic_string_view) to compare to

Returns 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.

Complexity

important

This section requires improvement. You can help by editing this doc page.

Exceptions

The overloads (2-3), (5-6), and (8-9) throw std::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 to std::basic_string_view.

If the operation would result in size() > max_size(), throws std::length_error.

If an exception is thrown for any reason, this function has no effect (strong exception guarantee).  (od C++11)

Possible implementation

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;
}

Notes

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

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

Example

Main.cpp
#include <cassert>
#include <string>
#include <iostream>

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) " <<
(
compare_result < 0 ? "Batman comes before Superman.\n" :
compare_result > 0 ? "Superman comes before Batman.\n" :
"Superman and Batman are the same.\n"
);

// 2) Compare substring with other string
compare_result = batman.compare(3, 3, superman);
std::cout << "2) " <<
(
compare_result < 0 ? "man comes before Superman.\n" :
compare_result > 0 ? "Superman comes before man.\n" :
"man and Superman are the same.\n"
);

// 3) Compare substring with other substring
compare_result = batman.compare(3, 3, superman, 5, 3);
std::cout << "3) " <<
(
compare_result < 0 ? "man comes before man.\n" :
compare_result > 0 ? "man comes before man.\n" :
"man and man are the same.\n"
);

// Compare substring with other substring
// defaulting to end of other string
assert(compare_result == batman.compare(3, 3, superman, 5));

// 4) Compare with char pointer
compare_result = batman.compare("Superman");
std::cout << "4) " <<
(
compare_result < 0 ? "Batman comes before Superman.\n" :
compare_result > 0 ? "Superman comes before Batman.\n" :
"Superman and Batman are the same.\n"
);

// 5) Compare substring with char pointer
compare_result = batman.compare(3, 3, "Superman");
std::cout << "5) " <<
(
compare_result < 0 ? "man comes before Superman.\n" :
compare_result > 0 ? "Superman comes before man.\n" :
"man and Superman are the same.\n"
);

// 6) Compare substring with char pointer substring
compare_result = batman.compare(0, 3, "Superman", 5);
std::cout << "6) " <<
(
compare_result < 0 ? "Bat comes before Super.\n" :
compare_result > 0 ? "Super comes before Bat.\n" :
"Super and Bat are the same.\n"
);
}
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.
This article originates from this CppReference page. It was likely altered for improvements or editors' preference. Click "Edit this page" to see all changes made to this document.
Hover to see the original license.

std::string compare() method

// (1) Const version only
constexpr int compare( const basic_string& str ) const noexcept;

// (2) Const version only
constexpr int compare( size_type pos1, size_type count1,
const basic_string& str ) const;

// (3) Const version only
constexpr int compare( size_type pos1, size_type count1,
const basic_string& str,
size_type pos2, size_type count2 = npos ) const;

// (4) Const version only
constexpr int compare( const CharT* s ) const;

// (5) Const version only
constexpr int compare( size_type pos1, size_type count1,
const CharT* s ) const;

// (6) Const version only
constexpr int compare( size_type pos1, size_type count1,
const CharT* s, size_type count2 ) const;

// (7) Const version only
template < class StringViewLike >
constexpr int compare( const StringViewLike& t ) const noexcept(/* see below */);

// (8) Const version only
template < class StringViewLike >
constexpr int compare( const StringViewLike& t ) const noexcept(/* see below */);

// (9) Const version only
template < class StringViewLike >
constexpr int compare( size_type pos1, size_type count1,
const StringViewLike& t,
size_type pos2, size_type count2 = npos) const;

Compares two character sequences.

  • (1) Compares this string to str.

  • (2) Compares a [ pos1, pos1 + count1 ) substring of this string to str.
    If count1 > size() - pos1 the substring is [ pos1, size() ).

  • (3) Compares a [pos1, pos1 + count1) substring of this string to a substring [ pos2, pos2 + count2) of str.
    If count1 > size() - pos1 the first substring is [ pos1, size() ). Likewise, count2 > str.size() - pos2 the second substring is [ pos2, str.size() ).

  • (4) Compares this string to the null-terminated character sequence beginning at the character pointed to by s with length Traits::length(s).

  • (5) Compares a [ pos1, pos1 + count1 ) substring of this string to the null-terminated character sequence beginning at the character pointed to by s with length Traits::length(s).
    If count1 > size() - pos1 the substring is [ pos1, size() ).

  • (6) Compares a [ pos1, pos1 + count1 ) substring of this string to the characters in the range [ s, s + count2 ).
    If count1 > size() - pos1 the substring is [ pos1, size() ).

    zanotuj

    The characters in the range [ s, s + count2) may include null characters.

  • (7) Implicitly converts t to a string view sv as if by std::basic_string_view<CharT, Traits> sv = t;, then compares this string to sv.

    Overload Resolution

    This overload participates in overload resolution only if std::is_convertible_v<const StringViewLike&, std::basic_string_view<CharT, Traits>> is true and std::is_convertible_v<const StringViewLike&, const CharT*> is false.

  • (8) Implicitly converts t to a string view sv as if by std::basic_string_view<CharT, Traits> sv = t;, then compares a [ pos1, pos1 + count1 ) substring of this string to sv, as if by std::basic_string_view<CharT, Traits>(*this).substr(pos1, count1).compare(sv).

    Overload Resolution

    This overload participates in overload resolution only if std::is_convertible_v<const StringViewLike&, std::basic_string_view<CharT, Traits>> is true and std::is_convertible_v<const StringViewLike&, const CharT*> is false.

  • (9) Implicitly converts t to a string view sv as if by std::basic_string_view<CharT, Traits> sv = t;, then compares a [ pos1, pos1 + count1 ) substring of this string to a substring [ pos2, pos2 + count2 ) of sv as if by std::basic_string_view<CharT, Traits>(*this).substr(pos1, count1).compare(sv.substr(pos2, count2)).

    Overload Resolution

    This overload participates in overload resolution only if std::is_convertible_v<const StringViewLike&, std::basic_string_view<CharT, Traits>> is true and std::is_convertible_v<const StringViewLike&, const CharT*> is false.

A character sequence consisting of count1 characters starting at data1 is compared to a character sequence consisting of count2 characters starting at data2 as follows:

  • (1) Calculate the number of characters to compare, as if by
    size_type rlen = std::min(count1, count2)
  • (2) Then compare the sequences as if by
    Traits::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:

important

This section requires improvement. You can help by editing this doc page.

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 to std::basic_string_view) to compare to

Returns 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.

Complexity

important

This section requires improvement. You can help by editing this doc page.

Exceptions

The overloads (2-3), (5-6), and (8-9) throw std::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 to std::basic_string_view.

If the operation would result in size() > max_size(), throws std::length_error.

If an exception is thrown for any reason, this function has no effect (strong exception guarantee).  (od C++11)

Possible implementation

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;
}

Notes

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

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

Example

Main.cpp
#include <cassert>
#include <string>
#include <iostream>

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) " <<
(
compare_result < 0 ? "Batman comes before Superman.\n" :
compare_result > 0 ? "Superman comes before Batman.\n" :
"Superman and Batman are the same.\n"
);

// 2) Compare substring with other string
compare_result = batman.compare(3, 3, superman);
std::cout << "2) " <<
(
compare_result < 0 ? "man comes before Superman.\n" :
compare_result > 0 ? "Superman comes before man.\n" :
"man and Superman are the same.\n"
);

// 3) Compare substring with other substring
compare_result = batman.compare(3, 3, superman, 5, 3);
std::cout << "3) " <<
(
compare_result < 0 ? "man comes before man.\n" :
compare_result > 0 ? "man comes before man.\n" :
"man and man are the same.\n"
);

// Compare substring with other substring
// defaulting to end of other string
assert(compare_result == batman.compare(3, 3, superman, 5));

// 4) Compare with char pointer
compare_result = batman.compare("Superman");
std::cout << "4) " <<
(
compare_result < 0 ? "Batman comes before Superman.\n" :
compare_result > 0 ? "Superman comes before Batman.\n" :
"Superman and Batman are the same.\n"
);

// 5) Compare substring with char pointer
compare_result = batman.compare(3, 3, "Superman");
std::cout << "5) " <<
(
compare_result < 0 ? "man comes before Superman.\n" :
compare_result > 0 ? "Superman comes before man.\n" :
"man and Superman are the same.\n"
);

// 6) Compare substring with char pointer substring
compare_result = batman.compare(0, 3, "Superman", 5);
std::cout << "6) " <<
(
compare_result < 0 ? "Bat comes before Super.\n" :
compare_result > 0 ? "Super comes before Bat.\n" :
"Super and Bat are the same.\n"
);
}
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.
This article originates from this CppReference page. It was likely altered for improvements or editors' preference. Click "Edit this page" to see all changes made to this document.
Hover to see the original license.