operator< (<string>)

Comprueba si el objeto string en el lado izquierdo del operador es menor que el objeto string en el lado derecho.

template<class CharType, class Traits, class Allocator>
   bool operator<(
      const basic_string<CharType, Traits, Allocator>& _Left,
      const basic_string<CharType, Traits, Allocator>& _Right
   );
template<class CharType, class Traits, class Allocator>
   bool operator<(
      const basic_string<CharType, Traits, Allocator>& _Left,
      const CharType *_Right
   );
template<class CharType, class Traits, class Allocator>
   bool operator<(
      const CharType *_Left,
      const basic_string<CharType, Traits, Allocator>& _Right
   );

Parámetros

  • _Left
    Una cadena de lenguaje c. o un objeto de basic_string tipo que se va a comparar.

  • _Right
    Una cadena de lenguaje c. o un objeto de basic_string tipo que se va a comparar.

Valor devuelto

TRUE si el objeto string en el lado izquierdo del operador es lexicográficamente menos que el objeto string en el lado derecho; si no Falso.

Comentarios

Una comparación lexicográfica entre cadenas las compara carácter a carácter hasta:

  • Encuentra dos caracteres correspondientes coinciden, y el resultado de la comparación se toma como resultado de la comparación entre cadenas.

  • No encuentra ningún desigualdad, pero una cadena tiene más caracteres que la otra, y la cadena más corta se considera menos que la cadena más larga.

  • No encuentra ningún desigualdad y la detecta que las cadenas tienen el mismo número de caracteres, por lo que las cadenas son iguales.

Ejemplo

// string_op_lt.cpp
// compile with: /EHsc
#include <string>
#include <iostream>

int main( )
{
   using namespace std;
   // Declaring an objects of type basic_string<char>
   string s1 ( "strict" );
   string s2 ( "strum" );
   cout << "The basic_string s1 = " << s1 << "." << endl;
   cout << "The basic_string s2 = " << s2 << "." << endl;

   // Declaring a C-style string
   char *s3 = "strict";
   cout << "The C-style string s3 = " << s3 << "." << endl;

   // First member function: comparison between left-side object
   // of type basic_string & right-side object of type basic_string
   if ( s1 < s2 )
      cout << "The string s1 is less than the string s2." << endl;
   else
      cout << "The string s1 is not less than the string s2." << endl;

   // Second member function: comparison between left-hand object
   // of type basic_string & right-hand object of C-syle string type
   if ( s1 < s3 )
      cout << "The string s1 is less than the string s3." << endl;
   else
      cout << "The string s1 is not less than the string s3." << endl;

   // Third member function: comparison between left-hand object
   // of C-syle string type & right-hand object of type basic_string
   if ( s3 < s2 )
      cout << "The string s3 is less than the string s2." << endl;
   else
      cout << "The string s3 is not less than the string s2." << endl;
}
  
  
  
  
  
  

Requisitos

encabezado: <cadena>

espacio de nombres: std

Vea también

Referencia

string::operator<