std::string compare() 方法
- 自 C++20 起
- 自 C++17 起
- 自 C++14 起
- 自 C++11 起
- 直到 C++11
// (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;
// (1) Const version only
int compare( const basic_string& str ) const noexcept;
// (2) Const version only
int compare( size_type pos1, size_type count1,
const basic_string& str ) const;
// (3) Const version only
int compare( size_type pos1, size_type count1,
const basic_string& str,
size_type pos2, size_type count2 = npos ) const;
// (4) Const version only
int compare( const CharT* s ) const;
// (5) Const version only
int compare( size_type pos1, size_type count1,
const CharT* s ) const;
// (6) Const version only
int compare( size_type pos1, size_type count1,
const CharT* s, size_type count2 ) const;
// (7) Const version only
template < class StringViewLike >
int compare( const StringViewLike& t ) const noexcept(/* see below */);
// (8) Const version only
template < class StringViewLike >
int compare( const StringViewLike& t ) const noexcept(/* see below */);
// (9) Const version only
template < class StringViewLike >
int compare( size_type pos1, size_type count1,
const StringViewLike& t,
size_type pos2, size_type count2 = npos) const;
// (1) Const version only
int compare( const basic_string& str ) const noexcept;
// (2) Const version only
int compare( size_type pos1, size_type count1,
const basic_string& str ) const;
// (3) Const version only
int compare( size_type pos1, size_type count1,
const basic_string& str,
size_type pos2, size_type count2 = npos ) const;
// (4) Const version only
int compare( const CharT* s ) const;
// (5) Const version only
int compare( size_type pos1, size_type count1,
const CharT* s ) const;
// (6) Const version only
int compare( size_type pos1, size_type count1,
const CharT* s, size_type count2 ) const;
// (1) Const version only
int compare( const basic_string& str ) const noexcept;
// (2) Const version only
int compare( size_type pos1, size_type count1,
const basic_string& str ) const;
// (3) Const version only
int compare( size_type pos1, size_type count1,
const basic_string& str,
size_type pos2, size_type count2 ) const;
// (4) Const version only
int compare( const CharT* s ) const;
// (5) Const version only
int compare( size_type pos1, size_type count1,
const CharT* s ) const;
// (6) Const version only
int compare( size_type pos1, size_type count1,
const CharT* s, size_type count2 ) const;
// (1) Const version only
int compare( const basic_string& str ) const;
// (2) Const version only
int compare( size_type pos1, size_type count1,
const basic_string& str ) const;
// (3) Const version only
int compare( size_type pos1, size_type count1,
const basic_string& str,
size_type pos2, size_type count2 ) const;
// (4) Const version only
int compare( const CharT* s ) const;
// (5) Const version only
int compare( size_type pos1, size_type count1,
const CharT* s ) const;
// (6) Const version only
int compare( size_type pos1, size_type count1,
const CharT* s, size_type count2 ) const;
比较两个字符序列。
-
(1) 将此字符串与
str
比较。 -
(2) 将此字符串的 [ pos1, pos1 + count1 ) 子字符串与
str
比较。
如果count1 > size() - pos1
,则子字符串为 [ pos1, size() )。 -
(3) 将此字符串的 [pos1, pos1 + count1) 子字符串与
str
的子字符串 [ pos2, pos2 + count2) 比较。
如果count1 > size() - pos1
,则第一个子字符串为 [ pos1, size() )。同样,如果count2 > str.size() - pos2
,则第二个子字符串为 [ 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 ) 中的字符进行比较。
如果count1 > size() - pos1
,则子字符串为 [ pos1, size() )。注意范围 [ s, s + count2) 中的字符可能包含空字符。
-
(7) 将
t
隐式转换为字符串视图sv
,如同通过std::basic_string_view<CharT, Traits> sv = t;
,然后将此字符串与sv
比较。重载决议此重载仅在
std::is_convertible_v<const StringViewLike&, std::basic_string_view<CharT, Traits>>
为true
且std::is_convertible_v<const StringViewLike&, const CharT*>
为false
时参与重载决议。 -
(8) 将
t
隐式转换为字符串视图sv
,如同通过std::basic_string_view<CharT, Traits> sv = t;
,然后将此字符串的 [ pos1, pos1 + count1 ) 子字符串与sv
比较,如同通过std::basic_string_view<CharT, Traits>(*this).substr(pos1, count1).compare(sv)
。重载决议此重载仅在
std::is_convertible_v<const StringViewLike&, std::basic_string_view<CharT, Traits>>
为true
且std::is_convertible_v<const StringViewLike&, const CharT*>
为false
时参与重载决议。 -
(9) 将
t
隐式转换为字符串视图sv
,如同通过std::basic_string_view<CharT, Traits> sv = t;
,然后将此字符串的 [ pos1, pos1 + count1 ) 子字符串与sv
的子字符串 [ pos2, pos2 + count2 ) 比较,如同通过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
时参与重载决议。
由 data1
开始的 count1
个字符序列与由 data2
开始的 count2
个字符序列按以下方式进行比较
- (1) 计算要比较的字符数量,如同通过
size_type rlen = std::min(count1, count2)
- (2) 然后比较序列,如同通过
Traits::compare(data1, data2, rlen)
对于标准字符串,此函数执行逐字符的字典顺序比较。
如果结果为零(字符序列到目前为止相等),则比较它们的大小,如下所示
本节需要改进。您可以通过编辑此文档页面来帮助我们。
参数
str
- 要比较的另一个字符串s
- 指向要比较的字符字符串的指针count1
- 要比较的此字符串的字符数量pos1
- 要比较的此字符串中第一个字符的位置count2
- 要比较的给定字符串的字符数量pos2
- 要比较的给定字符串中第一个字符的位置t
- 要比较的对象(可转换为std::basic_string_view
)
返回值
- 如果
*this
在字典顺序上出现在由参数指定的字符序列之前,则返回负值。 - 如果两个字符序列比较结果等价,则返回零。
- 如果
*this
在字典顺序上出现在由参数指定的字符序列之后,则返回正值。
复杂度
本节需要改进。您可以通过编辑此文档页面来帮助我们。
异常
重载 (2-3)、(5-6) 和 (8-9) 如果参数超出范围,则抛出 std::out_of_range
。
-
(7)
- 自 C++17 起
noexcept 规范
noexcept(std::is_nothrow_convertible_v<const T&, std::basic_string_view<CharT, Traits>>)
-
(8-9) 抛出从转换到
std::basic_string_view
抛出的任何异常。
如果操作导致 size() > max_size()
,则抛出std::length_error
。
可能的实现
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()
{
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"
);
}
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.