function template
<string>

std::relational operators (basic_string)

(1)
template <class charT, class traits, class Alloc>  bool operator== (const basic_string<charT,traits,Alloc>& lhs,                   const basic_string<charT,traits,Alloc>& rhs);template <class charT, class traits, class Alloc>  bool operator== (const charT* lhs, const basic_string<charT,traits,Alloc>& rhs);template <class charT, class traits, class Alloc>  bool operator== (const basic_string<charT,traits,Alloc>& lhs, const charT* rhs);
(2)
template <class charT, class traits, class Alloc>  bool operator!= (const basic_string<charT,traits,Alloc>& lhs,                   const basic_string<charT,traits,Alloc>& rhs);template <class charT, class traits, class Alloc>  bool operator!= (const charT* lhs, const basic_string<charT,traits,Alloc>& rhs);template <class charT, class traits, class Alloc>  bool operator!= (const basic_string<charT,traits,Alloc>& lhs, const charT* rhs);
(3)
template <class charT, class traits, class Alloc>  bool operator<  (const basic_string<charT,traits,Alloc>& lhs,                   const basic_string<charT,traits,Alloc>& rhs);template <class charT, class traits, class Alloc>  bool operator<  (const charT* lhs, const basic_string<charT,traits,Alloc>& rhs);template <class charT, class traits, class Alloc>  bool operator<  (const basic_string<charT,traits,Alloc>& lhs, const charT* rhs);
(4)
template <class charT, class traits, class Alloc>  bool operator<= (const basic_string<charT,traits,Alloc>& lhs,                   const basic_string<charT,traits,Alloc>& rhs);template <class charT, class traits, class Alloc>  bool operator<= (const charT* lhs, const basic_string<charT,traits,Alloc>& rhs);template <class charT, class traits, class Alloc>  bool operator<= (const basic_string<charT,traits,Alloc>& lhs, const charT* rhs);
(5)
template <class charT, class traits, class Alloc>  bool operator>  (const basic_string<charT,traits,Alloc>& lhs,                   const basic_string<charT,traits,Alloc>& rhs);template <class charT, class traits, class Alloc>  bool operator>  (const charT* lhs, const basic_string<charT,traits,Alloc>& rhs);template <class charT, class traits, class Alloc>  bool operator>  (const basic_string<charT,traits,Alloc>& lhs, const charT* rhs);
(6)
template <class charT, class traits, class Alloc>  bool operator>= (const basic_string<charT,traits,Alloc>& lhs,                   const basic_string<charT,traits,Alloc>& rhs);template <class charT, class traits, class Alloc>  bool operator>= (const charT* lhs, const basic_string<charT,traits,Alloc>& rhs);template <class charT, class traits, class Alloc>  bool operator>= (const basic_string<charT,traits,Alloc>& lhs, const charT* rhs);
(1)
template <class charT, class traits, class Alloc>  bool operator== (const basic_string<charT,traits,Alloc>& lhs,                   const basic_string<charT,traits,Alloc>& rhs) noexcept;template <class charT, class traits, class Alloc>  bool operator== (const charT* lhs, const basic_string<charT,traits,Alloc>& rhs);template <class charT, class traits, class Alloc>  bool operator== (const basic_string<charT,traits,Alloc>& lhs, const charT* rhs);
(2)
template <class charT, class traits, class Alloc>  bool operator!= (const basic_string<charT,traits,Alloc>& lhs,                   const basic_string<charT,traits,Alloc>& rhs) noexcept;template <class charT, class traits, class Alloc>  bool operator!= (const charT* lhs, const basic_string<charT,traits,Alloc>& rhs);template <class charT, class traits, class Alloc>  bool operator!= (const basic_string<charT,traits,Alloc>& lhs, const charT* rhs);
(3)
template <class charT, class traits, class Alloc>  bool operator<  (const basic_string<charT,traits,Alloc>& lhs,                   const basic_string<charT,traits,Alloc>& rhs) noexcept;template <class charT, class traits, class Alloc>  bool operator<  (const charT* lhs, const basic_string<charT,traits,Alloc>& rhs);template <class charT, class traits, class Alloc>  bool operator<  (const basic_string<charT,traits,Alloc>& lhs, const charT* rhs);
(4)
template <class charT, class traits, class Alloc>  bool operator<= (const basic_string<charT,traits,Alloc>& lhs,                   const basic_string<charT,traits,Alloc>& rhs) noexcept;template <class charT, class traits, class Alloc>  bool operator<= (const charT* lhs, const basic_string<charT,traits,Alloc>& rhs);template <class charT, class traits, class Alloc>  bool operator<= (const basic_string<charT,traits,Alloc>& lhs, const charT* rhs);
(5)
template <class charT, class traits, class Alloc>  bool operator>  (const basic_string<charT,traits,Alloc>& lhs,                   const basic_string<charT,traits,Alloc>& rhs) noexcept;template <class charT, class traits, class Alloc>  bool operator>  (const charT* lhs, const basic_string<charT,traits,Alloc>& rhs);template <class charT, class traits, class Alloc>  bool operator>  (const basic_string<charT,traits,Alloc>& lhs, const charT* rhs);
(6)
template <class charT, class traits, class Alloc>  bool operator>= (const basic_string<charT,traits,Alloc>& lhs,                   const basic_string<charT,traits,Alloc>& rhs) noexcept;template <class charT, class traits, class Alloc>  bool operator>= (const charT* lhs, const basic_string<charT,traits,Alloc>& rhs);template <class charT, class traits, class Alloc>  bool operator>= (const basic_string<charT,traits,Alloc>& lhs, const charT* rhs);
basic_string 的關係運算符
basic_string 物件lhsrhs 之間執行適當的比較操作。

這些函式使用 basic_string::compare 進行比較,這取決於其 字元特徵compare 成員。

這些運算子在標頭檔案 <string> 中過載。

引數

lhs, rhs
運算子左側和右側的引數,分別。
如果型別為charT*,則應指向一個以 null 結尾的字元序列。

示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// string comparisons
#include <iostream>
#include <vector>

int main ()
{
  std::string foo = "alpha";
  std::string bar = "beta";

  if (foo==bar) std::cout << "foo and bar are equal\n";
  if (foo!=bar) std::cout << "foo and bar are not equal\n";
  if (foo< bar) std::cout << "foo is less than bar\n";
  if (foo> bar) std::cout << "foo is greater than bar\n";
  if (foo<=bar) std::cout << "foo is less than or equal to bar\n";
  if (foo>=bar) std::cout << "foo is greater than or equal to bar\n";

  return 0;
}

輸出
foo and bar are not equal
foo is less than bar
foo is less than or equal to bar


返回值

true如果條件成立,則為 true,並且false否則為 false。

複雜度

未指定,但通常與 lhsrhs長度呈線性關係。

迭代器有效性

沒有變化。

資料競爭

可以訪問兩個物件 lhsrhs

異常安全

如果型別為 char* 的引數未指向以 null 結尾的字元序列,則會導致未定義行為
否則,如果丟擲異常,basic_string 中不會有任何更改(強保證)。
如果型別為 char* 的引數未指向以 null 結尾的字元序列,則會導致未定義行為
對於 basic_string 物件之間的操作,永遠不會丟擲異常(無丟擲保證)。
對於其他情況,如果丟擲異常,basic_string 中不會有任何更改(強保證)。

另見