operator==,!=,<,<=,>,>=,<=>(std::basic_string)
定義於標頭檔案 <string> |
||
比較兩個 basic_string 物件 |
||
template< class CharT, class Traits, class Alloc > bool operator==( const std::basic_string<CharT,Traits,Alloc>& lhs, |
(1) | (C++11 起無異常丟擲) (C++20 起為 constexpr) |
template< class CharT, class Traits, class Alloc > bool operator!=( const std::basic_string<CharT,Traits,Alloc>& lhs, |
(2) | (C++20 前) (C++11 起無異常丟擲) |
template< class CharT, class Traits, class Alloc > bool operator<( const std::basic_string<CharT,Traits,Alloc>& lhs, |
(3) | (C++20 前) (C++11 起無異常丟擲) |
template< class CharT, class Traits, class Alloc > bool operator<=( const std::basic_string<CharT,Traits,Alloc>& lhs, |
(4) | (C++20 前) (C++11 起無異常丟擲) |
template< class CharT, class Traits, class Alloc > bool operator>( const std::basic_string<CharT,Traits,Alloc>& lhs, |
(5) | (C++20 前) (C++11 起無異常丟擲) |
template< class CharT, class Traits, class Alloc > bool operator>=( const std::basic_string<CharT,Traits,Alloc>& lhs, |
(6) | (C++20 前) (C++11 起無異常丟擲) |
template< class CharT, class Traits, class Alloc > constexpr /*comp-cat*/ |
(7) | (C++20 起) |
比較 basic_string 物件與以 null 結尾的 T 陣列 |
||
template< class CharT, class Traits, class Alloc > bool operator==( const std::basic_string<CharT,Traits,Alloc>& lhs, |
(8) | (C++20 起為 constexpr) |
template< class CharT, class Traits, class Alloc > bool operator==( const CharT* lhs, |
(9) | (C++20 前) |
template< class CharT, class Traits, class Alloc > bool operator!=( const std::basic_string<CharT,Traits,Alloc>& lhs, |
(10) | (C++20 前) |
template< class CharT, class Traits, class Alloc > bool operator!=( const CharT* lhs, |
(11) | (C++20 前) |
template< class CharT, class Traits, class Alloc > bool operator<( const std::basic_string<CharT,Traits,Alloc>& lhs, |
(12) | (C++20 前) |
template< class CharT, class Traits, class Alloc > bool operator<( const CharT* lhs, |
(13) | (C++20 前) |
template< class CharT, class Traits, class Alloc > bool operator<=( const std::basic_string<CharT,Traits,Alloc>& lhs, |
(14) | (C++20 前) |
template< class CharT, class Traits, class Alloc > bool operator<=( const CharT* lhs, |
(15) | (C++20 前) |
template< class CharT, class Traits, class Alloc > bool operator>( const std::basic_string<CharT,Traits,Alloc>& lhs, |
(16) | (C++20 前) |
template< class CharT, class Traits, class Alloc > bool operator>( const CharT* lhs, |
(17) | (C++20 前) |
template< class CharT, class Traits, class Alloc > bool operator>=( const std::basic_string<CharT,Traits,Alloc>& lhs, |
(18) | (C++20 前) |
template< class CharT, class Traits, class Alloc > bool operator>=( const CharT* lhs, |
(19) | (C++20 前) |
template< class CharT, class Traits, class Alloc > constexpr /*comp-cat*/ |
(20) | (C++20 起) |
將字串的內容與另一個字串或以 null 結尾的 `CharT` 陣列進行比較。
所有比較均透過 compare() 成員函式完成(該函式本身是根據 `Traits::compare()` 定義的)
- 如果 lhs 和 rhs 的大小相等,並且 lhs 中的每個字元在 rhs 中的相同位置都有等效字元,則兩個字串相等。
- 排序比較是按字典順序進行的——比較由等效於 std::lexicographical_compare或 std::lexicographical_compare_three_way(從 C++20 開始) 的函式執行。
basic_string
物件。basic_string
物件和以 null 結尾的 CharT
陣列。
三路比較運算子 (/*comp-cat*/) 的返回型別是 Traits::comparison_category(如果該限定 ID 存在並表示一種型別),否則為 std::weak_ordering。如果 /*comp-cat*/ 不是比較類別型別,則程式格式錯誤。
|
(C++20 起) |
目錄 |
[編輯] 引數
lhs, rhs | - | 要比較內容的字串 |
[編輯] 返回值
[編輯] 複雜度
與字串大小呈線性關係。
[編輯] 注意
如果至少一個引數是 std::string、std::wstring、std::u8string、std::u16string 或 std::u32string 型別,則 |
(C++20 起) |
[編輯] 示例
本節不完整 原因:無示例 |
[編輯] 缺陷報告
下列更改行為的缺陷報告追溯地應用於以前出版的 C++ 標準。
缺陷報告 | 應用於 | 釋出時的行為 | 正確的行為 |
---|---|---|---|
LWG 2064 | C++11 | 接受兩個 basic_string 的過載是否為 noexcept 不一致;接受 CharT* 的過載為 noexcept 但可能引發 UB |
已使其一致; noexcept 已移除 |
LWG 3432 | C++20 | operator<=> 的返回型別不要求是比較類別型別 |
需要 |