C++ 参考手册
- C++11
- C++14
- C++17
- C++20
- C++ 编译器支持情况表
- 独立与宿主实现
- C++ 语言
- C++ 关键词
- 预处理器
- C++ 标准库头文件
- 具名要求
- 功能特性测试 (C++20)
- 工具库
- 类型支持(基本类型、RTTI、类型特性)
- 概念库 (C++20)
- 错误处理
- 动态内存管理
- 日期和时间工具
- 字符串库
- std::basic_string
- std::basic_string<CharT,Traits,Allocator>::npos
- std::basic_string<CharT,Traits,Allocator>::at
- std::hash (std::string, std::wstring, std::u16string, std::u32string, std::pmr::string, std::pmr::wstring, std::pmr::u16string, std::pmr::u32string)
- std::basic_string<CharT,Traits,Allocator>::assign
- std::basic_string<CharT,Traits,Allocator>::get_allocator
- std::basic_string<CharT,Traits,Allocator>::substr
- std::basic_string<CharT,Traits,Allocator>::basic_string
- std::basic_string<CharT,Traits,Allocator>::operator=
- std::basic_string<CharT,Traits,Allocator>::operator[]
- std::basic_string<CharT,Traits,Allocator>::front
- std::basic_string<CharT,Traits,Allocator>::back
- std::basic_string<CharT,Traits,Allocator>::data
- std::basic_string<CharT,Traits,Allocator>::c_str
- std::basic_string<CharT,Traits,Allocator>::operator basic_string_view
- std::basic_string<CharT,Traits,Allocator>::begin, std::basic_string<CharT,Traits,Allocator>::cbegin
- std::basic_string<CharT,Traits,Allocator>::end, std::basic_string<CharT,Traits,Allocator>::cend
- std::basic_string<CharT,Traits,Allocator>::rbegin, std::basic_string<CharT,Traits,Allocator>::crbegin
- std::basic_string<CharT,Traits,Allocator>::rend, std::basic_string<CharT,Traits,Allocator>::crend
- std::basic_string<CharT,Traits,Allocator>::empty
- std::basic_string<CharT,Traits,Allocator>::size, std::basic_string<CharT,Traits,Allocator>::length
- std::basic_string<CharT,Traits,Allocator>::max_size
- std::basic_string<CharT,Traits,Allocator>::reserve
- std::basic_string<CharT,Traits,Allocator>::capacity
- std::basic_string<CharT,Traits,Allocator>::shrink_to_fit
- std::basic_string<CharT,Traits,Allocator>::clear
- std::basic_string<CharT,Traits,Allocator>::insert
- std::basic_string<CharT,Traits,Allocator>::erase
- std::basic_string<CharT,Traits,Allocator>::push_back
- std::basic_string<CharT,Traits,Allocator>::pop_back
- std::basic_string<CharT,Traits,Allocator>::append
- std::basic_string<CharT,Traits,Allocator>::operator+=
- std::basic_string<CharT,Traits,Allocator>::compare
- std::basic_string<CharT,Traits,Allocator>::starts_with
- std::basic_string<CharT,Traits,Allocator>::ends_with
- std::basic_string<CharT,Traits,Allocator>::replace
- std::basic_string<CharT,Traits,Allocator>::copy
- std::basic_string<CharT,Traits,Allocator>::resize
- std::basic_string<CharT,Traits,Allocator>::swap
- std::basic_string<CharT,Traits,Allocator>::find
- std::basic_string<CharT,Traits,Allocator>::rfind
- std::basic_string<CharT,Traits,Allocator>::find_first_of
- std::basic_string<CharT,Traits,Allocator>::find_first_not_of
- std::basic_string<CharT,Traits,Allocator>::find_last_of
- std::basic_string<CharT,Traits,Allocator>::find_last_not_of
- std::operator+(std::basic_string)
- std::swap(std::basic_string)
- std::literals::string_literals::operator""s
- std::erase, std::erase_if (std::basic_string)
- operator<<,>>(std::basic_string)
- std::getline
- operator==,!=,<,<=,>,>=,<=>(std::basic_string)
- std::stoi, std::stol, std::stoll
- std::stoul, std::stoull
- std::stof, std::stod, std::stold
- std::to_string
- std::to_wstring
- std::basic_string 的推导指引
- std::basic_string_view
- 空终止字节字符串
- 空终止多字节字符串
- 空终止宽字符串
- std::char_traits
- 注释
- 容器库
- 迭代器库
- 范围库 (C++20)
- 算法库
- 数值库
- 输入/输出库
- 文件系统库
- 本地化库
- 正则表达式库
- 原子操作库
- 线程支持库
- 实验性 C++ 特性
- 有用的资源
- 索引
- std 符号索引
- 协程支持 (C++20)
- C++ 关键词
位置:首页 > C++ 参考手册 >字符串库 >std::basic_string > std::basic_string<CharT,Traits,Allocator>::compare
std::basic_string<CharT,Traits,Allocator>::compare
const basic_string& str,
const basic_string& str,
(C++20 前)
const basic_string& str,
const CharT* s ) const;
const CharT* s ) const;
const CharT* s, size_type count2 ) const;
const CharT* s, size_type count2 ) const;
int compare( const T& t ) const noexcept(/* see below */);
(C++20 前)
constexpr int compare( const T& t ) const noexcept(/* see below */);
int compare( size_type pos1, size_type count1,
(C++20 前)
constexpr int compare( size_type pos1, size_type count1,
int compare( size_type pos1, size_type count1,
const T& t,
(C++20 前)
constexpr int compare( size_type pos1, size_type count1,
const T& t,
比较二个字符序列。
[pos1, pos1+count1)
子串与 str 。若 count1 > size() - pos1 则子串为 [pos1, size())
。[pos1, pos1+count1)
子串与 str 的子串 [pos2, pos2+count2)
。若 count1 > size() - pos1 则第一子串为 [pos1, size())
。类似地若 count2 > str.size() - pos2 则第二子串为 [pos2, str.size())
。Traits::length(s)
的空终止字符序列。[pos1, pos1+count1)
子串与始于 s 所指向字符的长度为 Traits::length(s)
的空终止字符序列。若 count1 > size() - pos1 则子串为 [pos1, size())
。[pos1, pos1+count1)
子串与范围 [s, s + count2)
中的字符。(注意:范围 [s, s + count2)
中的字符可包含空字符。)t
为 string_view sv
,然后比较此 string 与 sv
。此重载仅若 std::is_convertible_v<const T&, std::basic_string_view<CharT, Traits>> 为 true 且 std::is_convertible_v<const T&, const CharT*> 为 false 才参与重载决议。t
为 string_view sv
,然后比较此 string 的 [pos1, pos1+count1)
子串与 sv
,如同用 std::basic_string_view<CharT, Traits>(data(), size()).substr(pos1, count1).compare(sv) 。此重载仅若 std::is_convertible_v<const T&, std::basic_string_view<CharT, Traits>> 为 true 且 std::is_convertible_v<const T&, const CharT*> 为 false 才参与重载决议。t
为 string_view sv
,然后比较此 string 的 [pos1, pos1+count1)
子串与 sv
的 [pos2, pos2+count2)
子串,如同用 std::basic_string_view<CharT, Traits>(data(), size()).substr(pos1, count1).compare(sv.substr(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 | - | 要比较的另一 string |
s | - | 指向要比较的字符串的指针 |
count1 | - | 此 string 要比较的字符数 |
pos1 | - | 此 string 中要比较的首字符的位置 |
count2 | - | 给定字符串要比较的字符数 |
pos2 | - | 给定字符串的要比较的首字符位置 |
t | - | 要比较的对象(可转换为 std::basic_string_view ) |
返回值
若 *this 在字典序中先出现于参数所指定的字符序列,则为正值。
若两个序列比较等价则为零。
若 *this 在字典序中后出现于参数所指定的字符序列,则为负值。
异常
接收名为 pos1
或 pos2
的参数的重载若参数在范围外则抛出 std::out_of_range 。
basic_string_view
的转换所抛的异常。可能的实现
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(); if (int result = traits_type::compare(data(), s.data(), std::min(lhs_sz, rhs_sz))) 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 。
缺陷报告
下列更改行为的缺陷报告追溯地应用于以前出版的 C++ 标准。
DR | 应用于 | 出版时的行为 | 正确行为 |
---|---|---|---|
LWG 2946 | C++17 | string_view 重载在某些情况下导致歧义
|
通过使之为模板来避免 |
示例
#include <cassert> #include <string> #include <iostream> int main() { // 1) 与另一 string 比较 { 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) 与另一 string 比较子串 { 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" ); // 与另一子串比较子串 // 默认到为另一 string 的末尾 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
参阅
(C++20 中移除)(C++20 中移除)(C++20 中移除)(C++20 中移除)(C++20 中移除)(C++20) |
以字典序比较两个字符串 (函数模板) |
返回子串 (公开成员函数) | |
定义字典序比较和字符串的散列 (类模板) | |
按照当前本地环境比较两个字符串 (函数) | |
当一个范围按字典顺序小于另一个范围时,返回 true (函数模板) |