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);
Relational operators for basic_string
Performs the appropriate comparison operation between the basic_string objects lhs and rhs.

The functions use basic_string::compare for the comparison, which depends on the compare member of its character traits.

These operators are overloaded in header <string>.

Parameters

lhs, rhs
Arguments to the left- and right-hand side of the operator, respectively.
If of type charT*, it shall point to a null-terminated character sequence.

Example

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;
}

Output:
foo and bar are not equal
foo is less than bar
foo is less than or equal to bar


Return Value

true if the condition holds, and false otherwise.

Complexity

Unspecified, but generally up to linear in both lhs and rhs's lengths.

Iterator validity

No changes.

Data races

Both objects, lhs and rhs, are accessed.

Exception safety

If an argument of type char* does not point to null-terminated character sequence, it causes undefined behavior.
Otherwise, if an exception is thrown, there are no changes in the basic_string (strong guarantee).
If an argument of type char* does not point to null-terminated character sequence, it causes undefined behavior.
For operations between basic_string objects, exceptions are never thrown (no-throw guarantee).
For other cases, if an exception is thrown, there are no changes in the basic_string (strong guarantee).

See also