名稱空間
變體
操作

operator==,!=,<,<=,>,>=,<=>(std::basic_string)

來自 cppreference.com
< cpp‎ | string‎ | basic_string
 
 
 
std::basic_string
常量
非成員函式
I/O
比較
operator==operator!=operator<operator>operator<=operator>=operator<=>
(until C++20)(until C++20)(until C++20)(until C++20)(until C++20)(C++20)
數值轉換
(C++11)(C++11)(C++11)
(C++11)(C++11) 
(C++11)(C++11)(C++11)
(C++11)
(C++11)
字面量
輔助類
推導指南 (C++17)
 
定義於標頭檔案 <string>
比較兩個 basic_string 物件
template< class CharT, class Traits, class Alloc >

bool operator==( const std::basic_string<CharT,Traits,Alloc>& lhs,

                 const std::basic_string<CharT,Traits,Alloc>& rhs );
(1) (C++11 起無異常丟擲)
(C++20 起為 constexpr)
template< class CharT, class Traits, class Alloc >

bool operator!=( const std::basic_string<CharT,Traits,Alloc>& lhs,

                 const std::basic_string<CharT,Traits,Alloc>& rhs );
(2) (C++20 前)
(C++11 起無異常丟擲)
template< class CharT, class Traits, class Alloc >

bool operator<( const std::basic_string<CharT,Traits,Alloc>& lhs,

                const std::basic_string<CharT,Traits,Alloc>& rhs );
(3) (C++20 前)
(C++11 起無異常丟擲)
template< class CharT, class Traits, class Alloc >

bool operator<=( const std::basic_string<CharT,Traits,Alloc>& lhs,

                 const std::basic_string<CharT,Traits,Alloc>& rhs );
(4) (C++20 前)
(C++11 起無異常丟擲)
template< class CharT, class Traits, class Alloc >

bool operator>( const std::basic_string<CharT,Traits,Alloc>& lhs,

                const std::basic_string<CharT,Traits,Alloc>& rhs );
(5) (C++20 前)
(C++11 起無異常丟擲)
template< class CharT, class Traits, class Alloc >

bool operator>=( const std::basic_string<CharT,Traits,Alloc>& lhs,

                 const std::basic_string<CharT,Traits,Alloc>& rhs );
(6) (C++20 前)
(C++11 起無異常丟擲)
template< class CharT, class Traits, class Alloc >

constexpr /*comp-cat*/
    operator<=>( const std::basic_string<CharT,Traits,Alloc>& lhs,

                 const std::basic_string<CharT,Traits,Alloc>& rhs ) noexcept;
(7) (C++20 起)
比較 basic_string 物件與以 null 結尾的 T 陣列
template< class CharT, class Traits, class Alloc >

bool operator==( const std::basic_string<CharT,Traits,Alloc>& lhs,

                 const CharT* rhs );
(8) (C++20 起為 constexpr)
template< class CharT, class Traits, class Alloc >

bool operator==( const CharT* lhs,

                 const std::basic_string<CharT,Traits,Alloc>& rhs );
(9) (C++20 前)
template< class CharT, class Traits, class Alloc >

bool operator!=( const std::basic_string<CharT,Traits,Alloc>& lhs,

                 const CharT* rhs );
(10) (C++20 前)
template< class CharT, class Traits, class Alloc >

bool operator!=( const CharT* lhs,

                 const std::basic_string<CharT,Traits,Alloc>& rhs );
(11) (C++20 前)
template< class CharT, class Traits, class Alloc >

bool operator<( const std::basic_string<CharT,Traits,Alloc>& lhs,

                const CharT* rhs );
(12) (C++20 前)
template< class CharT, class Traits, class Alloc >

bool operator<( const CharT* lhs,

                const std::basic_string<CharT,Traits,Alloc>& rhs );
(13) (C++20 前)
template< class CharT, class Traits, class Alloc >

bool operator<=( const std::basic_string<CharT,Traits,Alloc>& lhs,

                 const CharT* rhs );
(14) (C++20 前)
template< class CharT, class Traits, class Alloc >

bool operator<=( const CharT* lhs,

                 const std::basic_string<CharT,Traits,Alloc>& rhs );
(15) (C++20 前)
template< class CharT, class Traits, class Alloc >

bool operator>( const std::basic_string<CharT,Traits,Alloc>& lhs,

                const CharT* rhs );
(16) (C++20 前)
template< class CharT, class Traits, class Alloc >

bool operator>( const CharT* lhs,

                const std::basic_string<CharT,Traits,Alloc>& rhs );
(17) (C++20 前)
template< class CharT, class Traits, class Alloc >

bool operator>=( const std::basic_string<CharT,Traits,Alloc>& lhs,

                 const CharT* rhs );
(18) (C++20 前)
template< class CharT, class Traits, class Alloc >

bool operator>=( const CharT* lhs,

                 const std::basic_string<CharT,Traits,Alloc>& rhs );
(19) (C++20 前)
template< class CharT, class Traits, class Alloc >

constexpr /*comp-cat*/
    operator<=>( const std::basic_string<CharT,Traits,Alloc>& lhs,

                 const CharT* rhs );
(20) (C++20 起)

將字串的內容與另一個字串或以 null 結尾的 `CharT` 陣列進行比較。

所有比較均透過 compare() 成員函式完成(該函式本身是根據 `Traits::compare()` 定義的)

  • 如果 lhsrhs 的大小相等,並且 lhs 中的每個字元在 rhs 中的相同位置都有等效字元,則兩個字串相等。
1-7) 比較兩個 basic_string 物件。
8-20) 比較 basic_string 物件和以 null 結尾的 CharT 陣列。

三路比較運算子 (/*comp-cat*/) 的返回型別是 Traits::comparison_category(如果該限定 ID 存在並表示一種型別),否則為 std::weak_ordering。如果 /*comp-cat*/ 不是比較類別型別,則程式格式錯誤。

<, <=, >, >=!= 運算子分別從 operator<=>operator== 合成

(C++20 起)

目錄

[編輯] 引數

lhs, rhs - 要比較內容的字串

[編輯] 返回值

1-6,8-19) 如果相應的比較成立,則為 true,否則為 false
7,20) static_cast</*comp-cat*/>(lhs.compare(rhs) <=> 0)

[編輯] 複雜度

與字串大小呈線性關係。

[編輯] 注意

如果至少一個引數是 std::stringstd::wstringstd::u8stringstd::u16stringstd::u32string 型別,則 operator<=> 的返回型別是 std::strong_ordering

(C++20 起)

[編輯] 示例

[編輯] 缺陷報告

下列更改行為的缺陷報告追溯地應用於以前出版的 C++ 標準。

缺陷報告 應用於 釋出時的行為 正確的行為
LWG 2064 C++11 接受兩個 basic_string 的過載是否為 noexcept 不一致;
接受 CharT* 的過載為 noexcept 但可能引發 UB
已使其一致;
noexcept 已移除
LWG 3432 C++20 operator<=> 的返回型別不要求是比較類別型別 需要