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 中不会有任何更改(强保证)。

另见