Compartilhar via


basic_string::compare

Executa uma comparação com diferenciação de maiúsculas e minúsculas com uma cadeia de caracteres especificada para determinar se as duas cadeias de caracteres são iguais ou se um lexicographically é menor que o outro.

int compare(
    const basic_string<CharType, Traits, Allocator>& _Str
) const;
int compare(
    size_type _Pos1, 
    size_type _Num1,
    const basic_string<CharType, Traits, Allocator>& _Str
) const;
int compare(
    size_type _Pos1,
    size_type _Num1,
    const basic_string<CharType, Traits, Allocator>& _Str, 
    size_type _Off, 
    size_type _Count
) const;
int compare(
    const value_type* _Ptr
) const;
int compare(
    size_type _Pos1,
    size_type _Num1,
    const value_type* _Ptr
) const;
int compare(
    size_type _Pos1,
    size_type _Num1,
    const value_type* _Ptr
    size_type _Num2
) const;

Parâmetros

  • _Str
    A cadeia de caracteres que deve ser comparado à cadeia de caracteres dos operandos.

  • _Pos1
    O índice da cadeia de caracteres do operando na comparação que inicia.

  • _Num1
    O número máximo de caracteres de uma cadeia de caracteres do operando seja comparada.

  • _Num2
    O número máximo de caracteres de uma cadeia de caracteres de parâmetro para ser comparadas.

  • _Off
    O índice da cadeia de caracteres de parâmetro em que a comparação começa.

  • _Count
    O número máximo de caracteres de uma cadeia de caracteres de parâmetro para ser comparadas.

  • _Ptr
    C -- a cadeia de caracteres a ser comparado à cadeia de caracteres dos operandos.

Valor de retorno

Um valor negativo se a cadeia de caracteres de operando for menor do que a cadeia de caracteres de parâmetro; zero se as duas cadeias de caracteres são iguais; ou um valor positivo se a cadeia de caracteres de operando for maior do que a cadeia de caracteres de parâmetro.

Comentários

As funções de membro de compare comparam ou todo ou parte das cadeias de caracteres de parâmetro e do segundo operando que usado no.

A comparação executada diferencia maiúsculas de minúsculas.

Exemplo

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

int main( ) 
{
   using namespace std;

   // The first member function compares
   // an operand string to a parameter string
   int comp1;
   string s1o ( "CAB" );
   string s1p ( "CAB" );
   cout << "The operand string is: " << s1o << endl;
   cout << "The parameter string is: " << s1p << endl;
   comp1 = s1o.compare ( s1p );
   if ( comp1 < 0 )
      cout << "The operand string is less than "
           << "the parameter string." << endl;
   else if ( comp1 == 0 )
      cout << "The operand string is equal to "
           << "the parameter string." << endl;
   else
      cout << "The operand string is greater than "
           << "the parameter string." << endl;
   cout << endl;

   // The second member function compares part of
   // an operand string to a parameter string
   int comp2a, comp2b;
   string s2o ( "AACAB" );
   string s2p ( "CAB" );
   cout << "The operand string is: " << s2o << endl;
   cout << "The parameter string is: " << s2p << endl;
   comp2a = s2o.compare (  2 , 3 , s2p );
   if ( comp2a < 0 )
      cout << "The last three characters of "
           << "the operand string\n are less than "
           << "the parameter string." << endl;
   else if ( comp2a == 0 )
      cout << "The last three characters of "
           << "the operand string\n are equal to "
           << "the parameter string." << endl;
   else
      cout << "The last three characters of "
           << "the operand string\n is greater than "
           << "the parameter string." << endl;

   comp2b = s2o.compare (  0 , 3 , s2p );
   if ( comp2b < 0 )
      cout << "The first three characters of "
           << "the operand string\n are less than "
           << "the parameter string." << endl;
   else if ( comp2b == 0 )
      cout << "The first three characters of "
           << "the operand string\n are equal to "
           << "the parameter string." << endl;
   else
      cout << "The first three characters of "
           << "the operand string\n is greater than "
           << "the parameter string." << endl;
   cout << endl;

   // The third member function compares part of
   // an operand string to part of a parameter string
   int comp3a;
   string s3o ( "AACAB" );
   string s3p ( "DCABD" );
   cout << "The operand string is: " << s3o << endl;
   cout << "The parameter string is: " << s3p << endl;
   comp3a = s3o.compare (  2 , 3 , s3p , 1 , 3 );
   if ( comp3a < 0 )
      cout << "The three characters from position 2 of "
           << "the operand string are less than\n "
           << "the 3 characters parameter string " 
           << "from position 1." << endl;
   else if ( comp3a == 0 )
      cout << "The three characters from position 2 of "
           << "the operand string are equal to\n "
           << "the 3 characters parameter string " 
           << "from position 1." << endl;
   else
      cout << "The three characters from position 2 of "
           << "the operand string is greater than\n "
           << "the 3 characters parameter string " 
           << "from position 1." << endl;
   cout << endl;

   // The fourth member function compares
   // an operand string to a parameter C-string
   int comp4a;
   string s4o ( "ABC" );
   const char* cs4p = "DEF";
   cout << "The operand string is: " << s4o << endl;
   cout << "The parameter C-string is: " << cs4p << endl;
   comp4a = s4o.compare ( cs4p );
   if ( comp4a < 0 )
      cout << "The operand string is less than "
           << "the parameter C-string." << endl;
   else if ( comp4a == 0 )
      cout << "The operand string is equal to "
           << "the parameter C-string." << endl;
   else
      cout << "The operand string is greater than "
           << "the parameter C-string." << endl;
   cout << endl;

   // The fifth member function compares part of
   // an operand string to a parameter C-string
   int comp5a;
   string s5o ( "AACAB" );
   const char* cs5p = "CAB";
   cout << "The operand string is: " << s5o << endl;
   cout << "The parameter string is: " << cs5p << endl;
   comp5a = s5o.compare (  2 , 3 , s2p );
   if ( comp5a < 0 )
      cout << "The last three characters of "
           << "the operand string\n are less than "
           << "the parameter C-string." << endl;
   else if ( comp5a == 0 )
      cout << "The last three characters of "
           << "the operand string\n are equal to "
           << "the parameter C-string." << endl;
   else
      cout << "The last three characters of "
           << "the operand string\n is greater than "
           << "the parameter C-string." << endl;
   cout << endl;

   // The sixth member function compares part of
   // an operand string to part of an equal length of
   // a parameter C-string
   int comp6a;
   string s6o ( "AACAB" );
   const char* cs6p = "ACAB";
   cout << "The operand string is: " << s6o << endl;
   cout << "The parameter C-string is: " << cs6p << endl;
   comp6a = s6o.compare (  1 , 3 , cs6p , 3 );
   if ( comp6a < 0 )
      cout << "The 3 characters from position 1 of "
           << "the operand string are less than\n "
           << "the first 3 characters of the parameter C-string." 
           << endl;
   else if ( comp6a == 0 )
      cout << "The 3 characters from position 2 of "
           << "the operand string are equal to\n "
           << "the first 3 characters of the parameter C-string." 
           <<  endl;
   else
      cout << "The 3 characters from position 2 of "
           << "the operand string is greater than\n "
           << "the first 3 characters of the parameter C-string." 
           << endl;
   cout << endl;
}
  
  
  
  
  
  
  

Requisitos

Cabeçalho: <string>

namespace: STD

Consulte também

Referência

basic_string Class