Partilhar via


char_traits Struct

A char_traits struct descreve atributos associados a um caractere.

Sintaxe

template <class CharType>
struct char_traits;

Parâmetros

CharType
O tipo de dados do elemento.

Observações

O modelo struct descreve vários traços de caractere para o tipo CharType. O modelo de classe basic_string , bem como vários modelos de classe iostream, incluindo basic_ios, usam essas informações para manipular elementos do tipo CharType. Este tipo de elemento não deve exigir uma construção ou destruição explícitas. Ele deve fornecer um construtor padrão, um construtor de cópia e um operador de atribuição, com a semântica esperada. Uma cópia bit a bit deve ter o mesmo efeito que uma atribuição. Nenhuma das funções de membro do struct char_traits pode lançar exceções.

Typedefs (definições de tipos)

Nome do tipo Descrição
char_type Um tipo de personagem.
int_type Um tipo inteiro que pode representar um caractere de tipo char_type ou um caractere de fim de arquivo (EOF).
off_type Um tipo inteiro que pode representar deslocamentos entre posições em um fluxo.
pos_type Um tipo inteiro que pode representar posições em um fluxo.
state_type Um tipo que representa o estado de conversão em para caracteres multibyte em um fluxo.

Funções de membro

Função de membro Descrição
atribuir Atribui um valor de caractere a outro.
comparar preços Compara até um número especificado de caracteres em duas cadeias de caracteres.
cópia Copia um número especificado de caracteres de uma cadeia de caracteres para outra. Preterido. Use char_traits::_Copy_s em vez disso.
_Copy_s Copia um número especificado de caracteres de uma cadeia de caracteres para outra.
Eof Retorna o caractere de fim de arquivo (EOF).
QE Testa se dois char_type caracteres são iguais.
eq_int_type Testa se dois caracteres representados como int_types são iguais.
encontrar Procura a primeira ocorrência de um caractere especificado em um intervalo de caracteres.
comprimento Retorna o comprimento de uma cadeia de caracteres.
lt Testa se um caractere é menor que outro.
mover Copia um número especificado de caracteres em uma sequência para outra possível sequência sobreposta. Preterido. Use char_traits::_Move_s em vez disso.
_Move_s Copia um número especificado de caracteres em uma sequência para outra possível sequência sobreposta.
not_eof Testa se um caractere é o caractere de fim de arquivo (EOF).
to_char_type Converte um int_type caractere no caractere correspondente char_type e retorna o resultado.
to_int_type Converte um char_type caractere no caractere correspondente int_type e retorna o resultado.

Requerimentos

Cabeçalho:<string>

Espaço de nomes: std

char_traits::atribuir

Atribui um valor de caractere a outro ou a um intervalo de elementos em uma cadeia de caracteres.

static void assign(char_type& _CharTo,
    const char_type& _CharFrom);

static char_type *assign(char_type* strTo,
    size_t _Num,
    char_type _CharFrom);

Parâmetros

_CharFrom O caractere cujo valor deve ser atribuído.

_CharTo
O elemento ao qual deve ser atribuído o valor do caractere.

strTo
A cadeia de caracteres ou matriz de caracteres cujos elementos iniciais devem ser atribuídos valores de caracteres.

_Num
O número de elementos aos quais serão atribuídos valores.

Valor de retorno

A função de segundo membro retorna um ponteiro para a cadeia de caracteres cujos primeiros elementos _Num foram atribuídos valores de _CharFrom.

Exemplo

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

int main( )
{
   using namespace std;

   // The first member function assigning
   // one character value to another character
   char ChTo = 't';
   const char ChFrom = 'f';
   cout << "The initial characters ( ChTo , ChFrom ) are: ( "
        << ChTo << " , " << ChFrom << " )." << endl;
   char_traits<char>::assign ( ChTo , ChFrom );
   cout << "After assigning, the characters ( ChTo , ChFrom ) are: ( "
        << ChTo << " , " << ChFrom << " )." << endl << endl;

   // The second member function assigning
   // character values to initial part of a string
   char_traits<char>::char_type s1[] = "abcd-1234-abcd";
   char_traits<char>::char_type* result1;
   cout << "The target string s1 is: " << s1 << endl;
   result1 = char_traits<char>::assign ( s1 , 4 , 'f' );
   cout << "The result1 = assign ( s1 , 4 , 'f' ) is: "
        << result1 << endl;
}
The initial characters ( ChTo , ChFrom ) are: ( t , f ).
After assigning, the characters ( ChTo , ChFrom ) are: ( f , f ).

The target string s1 is: abcd-1234-abcd
The result1 = assign ( s1 , 4 , 'f' ) is: ffff-1234-abcd

char_traits::char_type

Um tipo de personagem.

typedef CharType char_type;

Observações

O tipo é um sinônimo para o parâmetro de modelo CharType.

Exemplo

Consulte o exemplo para cópia para obter um exemplo de como declarar e usar char_typeo .

char_traits::comparar

Compara até um número especificado de caracteres em duas cadeias de caracteres.

static int compare(const char_type* str1,
    const char_type* str2,
    size_t _Num);

Parâmetros

STR1
A primeira de duas cordas a serem comparadas entre si.

STR2
A segunda de duas cordas a serem comparadas entre si.

_Num
O número de elementos nas cadeias de caracteres a serem comparadas.

Valor de retorno

Um valor negativo se a primeira cadeia de caracteres for menor que a segunda, 0 se as duas cadeias forem iguais ou um valor positivo se a primeira cadeia for maior que a segunda cadeia de caracteres.

Observações

A comparação entre as cadeias de caracteres é feita elemento por elemento, primeiro testando a igualdade e, em seguida, se um par de elementos na sequência não é igual, eles são testados para menos de.

Se duas cadeias de caracteres comparam iguais em um intervalo, mas uma é maior do que a outra, então a menor das duas é menor do que a mais longa.

Exemplo

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

int main() {
   using namespace std;

   char_traits<char>::char_type* s1 = "CAB";
   char_traits<char>::char_type* s2 = "ABC";
   char_traits<char>::char_type* s3 = "ABC";
   char_traits<char>::char_type* s4 = "ABCD";

   cout << "The string s1 is: " << s1 << endl;
   cout << "The string s2 is: " << s2 << endl;
   cout << "The string s3 is: " << s3 << endl;
   cout << "The string s4 is: " << s4 << endl;

   int comp1, comp2, comp3, comp4;
   comp1 = char_traits<char>::compare ( s1 , s2 , 2 );
   comp2 = char_traits<char>::compare ( s2 , s3 , 3 );
   comp3 = char_traits<char>::compare ( s3 , s4 , 4 );
   comp4 = char_traits<char>::compare ( s4 , s3 , 4 );
   cout << "compare ( s1 , s2 , 2 ) = " << comp1 << endl;
   cout << "compare ( s2 , s3 , 3 ) = " << comp2 << endl;
   cout << "compare ( s3 , s4 , 4 ) = " << comp3 << endl;
   cout << "compare ( s4 , s3 , 4 ) = " << comp4 << endl;
}

char_traits::cópia

Copia um número especificado de caracteres de uma cadeia de caracteres para outra.

Esse método é potencialmente inseguro, pois depende do chamador para verificar se os valores passados estão corretos. Em vez disso, considere usar char_traits::_Copy_s .

static char_type *copy(char_type* _To,
    const char_type* _From,
    size_t _Num);

Parâmetros

_Para
O elemento no início da cadeia de caracteres ou matriz de caracteres destinada a receber a sequência copiada de caracteres.

_De
O elemento no início da cadeia de caracteres de origem ou matriz de caracteres a ser copiado.

_Num
O número de elementos a serem copiados.

Valor de retorno

O primeiro elemento copiado para a cadeia de caracteres ou matriz de caracteres direcionada para receber a sequência copiada de caracteres.

Observações

As sequências de caracteres de origem e de destino não devem se sobrepor.

Exemplo

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

int main( )
{
   using namespace std;

   char_traits<char>::char_type s1[] = "abcd-1234-abcd";
   char_traits<char>::char_type s2[] = "ABCD-1234";
   char_traits<char>::char_type* result1;
   cout << "The source string is: " << s1 << endl;
   cout << "The destination string is: " << s2 << endl;
   // Note: char_traits::copy is potentially unsafe, consider
   // using char_traits::_Copy_s instead.
   result1 = char_traits<char>::copy ( s1 , s2 , 4 );  // C4996
   cout << "The result1 = copy ( s1 , s2 , 4 ) is: "
        << result1 << endl;
}
The source string is: abcd-1234-abcd
The destination string is: ABCD-1234
The result1 = copy ( s1 , s2 , 4 ) is: ABCD-1234-abcd

char_traits::_Copy_s

Copia um número especificado de caracteres de uma cadeia de caracteres para outra.

static char_type *_Copy_s(
    char_type* dest,
    size_t dest_size,
    const char_type* _From,
    size_t count);

Parâmetros

dest
A cadeia de caracteres ou matriz de caracteres direcionada para receber a sequência copiada de caracteres.

dest_size
O tamanho do dest. Se char_type for char, então este tamanho é em bytes. Se char_type é wchar_t, então este tamanho é em palavras.

_De
A cadeia de caracteres de origem ou matriz de caracteres a ser copiada.

contagem
O número de elementos a serem copiados.

Valor de retorno

A cadeia de caracteres ou matriz de caracteres direcionada para receber a sequência copiada de caracteres.

Observações

As sequências de caracteres de origem e de destino não devem se sobrepor.

Exemplo

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

int main( )
{
    using namespace std;

    char_traits<char>::char_type s1[] = "abcd-1234-abcd";
    char_traits<char>::char_type s2[] = "ABCD-1234";
    char_traits<char>::char_type* result1;
    cout << "The source string is: " << s1 << endl;
    cout << "The destination string is: " << s2 << endl;
    result1 = char_traits<char>::_Copy_s(s1,
        char_traits<char>::length(s1), s2, 4);
    cout << "The result1 = _Copy_s(s1, "
         << "char_traits<char>::length(s1), s2, 4) is: "
         << result1 << endl;
}
The source string is: abcd-1234-abcd
The destination string is: ABCD-1234
The result1 = _Copy_s(s1, char_traits<char>::length(s1), s2, 4) is: ABCD-1234-abcd

char_traits::eof

Retorna o caractere de fim de arquivo (EOF).

static int_type eof();

Valor de retorno

O caráter EOF.

Observações

Um valor que representa o fim do arquivo (como EOF ou WEOF).

O padrão C++ afirma que esse valor não deve corresponder a um valor válido char_type . O compilador do Microsoft C++ impõe essa restrição para o tipo char, mas não para o tipo wchar_t. O exemplo abaixo demonstra isso.

Exemplo

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

int main()
{
    using namespace std;

    char_traits<char>::char_type ch1 = 'x';
    char_traits<char>::int_type int1;
    int1 = char_traits<char>::to_int_type(ch1);
    cout << "char_type ch1 is '" << ch1 << "' and corresponds to int_type "
         << int1 << "." << endl << endl;

    char_traits<char>::int_type int2 = char_traits<char>::eof();
    cout << "The eof marker for char_traits<char> is: " << int2 << endl;

    char_traits<wchar_t>::int_type int3 = char_traits<wchar_t>::eof();
    cout << "The eof marker for char_traits<wchar_t> is: " << int3 << endl;
}
char_type ch1 is 'x' and corresponds to int_type 120.

The eof marker for char_traits<char> is: -1
The eof marker for char_traits<wchar_t> is: 65535

char_traits::eq

Testa se dois char_type caracteres são iguais.

static bool eq(const char_type& _Ch1, const char_type& _Ch2);

Parâmetros

_Ch1
O primeiro de dois caracteres a ser testado para a igualdade.

_Ch2
O segundo de dois caracteres a serem testados para a igualdade.

Valor de retorno

true se o primeiro caractere for igual ao segundo; caso contrário false.

Exemplo

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

int main( )
{
   using namespace std;

   char_traits<char>::char_type ch1 =  'x';
   char_traits<char>::char_type ch2 =  'y';
   char_traits<char>::char_type ch3 =  'x';

   // Testing for equality
   bool b1 = char_traits<char>::eq ( ch1 , ch2 );
   if ( b1 )
      cout << "The character ch1 is equal "
           << "to the character ch2." << endl;
   else
      cout << "The character ch1 is not equal "
           << "to the character ch2." << endl;

   // An equivalent and alternatively test procedure
   if ( ch1 == ch3 )
      cout << "The character ch1 is equal "
           << "to the character ch3." << endl;
   else
      cout << "The character ch1 is not equal "
           << "to the character ch3." << endl;
}
The character ch1 is not equal to the character ch2.
The character ch1 is equal to the character ch3.

char_traits::eq_int_type

Testa se dois caracteres representados como int_types são iguais ou não.

static bool eq_int_type(const int_type& _Ch1, const int_type& _Ch2);

Parâmetros

_Ch1
O primeiro dos dois caracteres a ser testado para a igualdade como int_types.

_Ch2
O segundo dos dois caracteres a ser testado para a igualdade como int_types.

Valor de retorno

true se o primeiro caractere for igual ao segundo; caso contrário false.

Exemplo

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

int main( )
{
   using namespace std;

   char_traits<char>::char_type ch1 =  'x';
   char_traits<char>::char_type ch2 =  'y';
   char_traits<char>::char_type ch3 =  'x';

   // Converting from char_type to int_type
   char_traits<char>::int_type int1, int2 , int3;
   int1 =char_traits<char>:: to_int_type ( ch1 );
   int2 =char_traits<char>:: to_int_type ( ch2 );
   int3 =char_traits<char>:: to_int_type ( ch3 );

   cout << "The char_types and corresponding int_types are:"
        << "\n    ch1 = " << ch1 << " corresponding to int1 = "
        << int1 << "."
        << "\n    ch2 = " << ch2 << " corresponding to int1 = "
        << int2 << "."
        << "\n    ch3 = " << ch3 << " corresponding to int1 = "
        << int3 << "." << endl << endl;

   // Testing for equality of int_type representations
   bool b1 = char_traits<char>::eq_int_type ( int1 , int2 );
   if ( b1 )
      cout << "The int_type representation of character ch1\n "
           << "is equal to the int_type representation of ch2."
           << endl;
   else
      cout << "The int_type representation of character ch1\n is "
           << "not equal to the int_type representation of ch2."
           << endl;

   // An equivalent and alternatively test procedure
   if ( int1 == int3 )
      cout << "The int_type representation of character ch1\n "
           << "is equal to the int_type representation of ch3."
           << endl;
   else
      cout << "The int_type representation of character ch1\n is "
           << "not equal to the int_type representation of ch3."
           << endl;
}
The char_types and corresponding int_types are:
    ch1 = x corresponding to int1 = 120.
    ch2 = y corresponding to int1 = 121.
    ch3 = x corresponding to int1 = 120.

The int_type representation of character ch1
is not equal to the int_type representation of ch2.
The int_type representation of character ch1
is equal to the int_type representation of ch3.

char_traits::encontrar

Procura a primeira ocorrência de um caractere especificado em um intervalo de caracteres.

static const char_type* find(const char_type* str,
    size_t _Num,
    const char_type& _Ch);

Parâmetros

STR
O primeiro caractere na cadeia de caracteres a ser pesquisado.

_Num
O número de posições, contadas a partir da primeira, no intervalo a ser pesquisado.

_Ch
O personagem a ser pesquisado no intervalo.

Valor de retorno

Um ponteiro para a primeira ocorrência do caractere especificado no intervalo se uma correspondência for encontrada; caso contrário, um ponteiro nulo.

Exemplo

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

int main( )
{
   using namespace std;

   const char* s1 = "f2d-1234-abcd";
   const char* result1;
   cout << "The string to be searched is: " << s1 << endl;

   // Searching for a 'd' in the first 6 positions of string s1
   result1 = char_traits<char>::find ( s1 , 6 , 'd');
   cout << "The character searched for in s1 is: "
        << *result1 << endl;
   cout << "The string beginning with the first occurrence\n "
        << "of the character 'd' is: " << result1 << endl;

   // When no match is found the NULL value is returned
   const char* result2;
   result2 = char_traits<char>::find ( s1 , 3 , 'a');
   if ( result2 == NULL )
      cout << "The result2 of the search is NULL." << endl;
   else
      cout << "The result2 of the search  is: " << result1
           << endl;
}
The string to be searched is: f2d-1234-abcd
The character searched for in s1 is: d
The string beginning with the first occurrence
of the character 'd' is: d-1234-abcd
The result2 of the search is NULL.

char_traits::int_type

Um tipo inteiro que pode representar um caractere de tipo char_type ou um caractere de fim de arquivo (EOF).

typedef long int_type;

Observações

Deve ser possível digitar um valor de tipo CharType para int_type depois voltar para CharType sem alterar o valor original.

Exemplo

Veja o exemplo para eq_int_type para um exemplo de como declarar e usar int_typeo .

char_traits::comprimento

Retorna o comprimento de uma cadeia de caracteres.

static size_t length(const char_type* str);

Parâmetros

STR
A cadeia de caracteres C cujo comprimento deve ser medido.

Valor de retorno

O número de elementos na sequência que está sendo medida, não incluindo o terminador nulo.

Exemplo

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

int main( )
{
   using namespace std;
   const char* str1= "Hello";
   cout << "The C-string str1 is: " << str1 << endl;

   size_t lenStr1;
   lenStr1 = char_traits<char>::length ( str1 );
   cout << "The length of C-string str1 is: "
        << lenStr1 << "." << endl;
}
The C-string str1 is: Hello
The length of C-string str1 is: 5.

char_traits::lt

Testa se um caractere é menor que outro.

static bool lt(const char_type& _Ch1, const char_type& _Ch2);

Parâmetros

_Ch1
O primeiro de dois caracteres a ser testado por menos de.

_Ch2
O segundo de dois caracteres a ser testado por menos de.

Valor de retorno

true se o primeiro caractere for menor que o segundo; caso contrário false.

Exemplo

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

int main( )
{
   using namespace std;
   char_traits<char>::char_type ch1 =  'x';
   char_traits<char>::char_type ch2 =  'y';
   char_traits<char>::char_type ch3 =  'z';

   // Testing for less than
   bool b1 = char_traits<char>::lt ( ch1 , ch2 );
   if ( b1 )
      cout << "The character ch1 is less than "
           << "the character ch2." << endl;
   else
      cout << "The character ch1 is not less "
           << "than the character ch2." << endl;

   // An equivalent and alternatively test procedure
   if ( ch3 <  ch2 )
      cout << "The character ch3 is less than "
           << "the character ch2." << endl;
   else
      cout << "The character ch3 is not less "
           << "than the character ch2." << endl;
}
The character ch1 is less than the character ch2.
The character ch3 is not less than the character ch2.

char_traits::move

Copia um número especificado de caracteres em uma sequência para outra, possivelmente sobreposta.

Esse método é potencialmente inseguro, pois depende do chamador para verificar se os valores passados estão corretos. Em vez disso, considere usar char_traits::_Move_s .

static char_type *move(char_type* _To,
    const char_type* _From,
    size_t _Num);

Parâmetros

_Para
O elemento no início da cadeia de caracteres ou matriz de caracteres destinada a receber a sequência copiada de caracteres.

_De
O elemento no início da cadeia de caracteres de origem ou matriz de caracteres a ser copiado.

_Num
O número de elementos a serem copiados da cadeia de caracteres de origem.

Valor de retorno

O primeiro elemento _To copiado para a cadeia de caracteres ou matriz de caracteres direcionada para receber a sequência copiada de caracteres.

Observações

A origem e o destino podem sobrepor-se.

Exemplo

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

int main( )
{
   using namespace std;

   char_traits<char>::char_type sFrom1[] =  "abcd-1234-abcd";
   char_traits<char>::char_type sTo1[] =  "ABCD-1234";
   char_traits<char>::char_type* result1;
   cout << "The source string sFrom1 is: " << sFrom1 << endl;
   cout << "The destination stringsTo1 is: " << sTo1 << endl;
   // Note: char_traits::move is potentially unsafe, consider
   // using char_traits::_Move_s instead.
   result1 = char_traits<char>::move ( sTo1 ,  sFrom1 , 4 );  // C4996
   cout << "The result1 = move ( sTo1 , sFrom1 , 4 ) is: "
        << result1 << endl << endl;

   // When source and destination overlap
   char_traits<char>::char_type sToFrom2[] = "abcd-1234-ABCD";
   char_traits<char>::char_type* result2;
   cout << "The source/destination string sToFrom2 is: "
        << sToFrom2 << endl;
   const char* findc = char_traits<char>::find ( sToFrom2 , 4 , 'c' );
   // Note: char_traits::move is potentially unsafe, consider
   // using char_traits::_Move_s instead.
   result2 = char_traits<char>::move ( sToFrom2 , findc , 8 );  // C4996
   cout << "The result2 = move ( sToFrom2 , findc , 8 ) is: "
        << result2 << endl;
}
The source string sFrom1 is: abcd-1234-abcd
The destination stringsTo1 is: ABCD-1234
The result1 = move ( sTo1 , sFrom1 , 4 ) is: abcd-1234

The source/destination string sToFrom2 is: abcd-1234-ABCD
The result2 = move ( sToFrom2 , findc , 8 ) is: cd-1234-4-ABCD

char_traits::_Move_s

Copia um número especificado de caracteres em uma sequência para outra, possivelmente sobreposta.

static char_type *_Move_s(
    char_type* dest,
    size_t dest_size,
    const char_type* _From,
    size_t count);

Parâmetros

dest
O elemento no início da cadeia de caracteres ou matriz de caracteres destinada a receber a sequência copiada de caracteres.

dest_size
O tamanho do dest. Se char_type for char, então isso é em bytes. Se char_type é wchar_t, então isto é em palavras.

_De
O elemento no início da cadeia de caracteres de origem ou matriz de caracteres a ser copiado.

contagem
O número de elementos a serem copiados da cadeia de caracteres de origem.

Valor de retorno

O primeiro elemento dest copiado para a cadeia de caracteres ou matriz de caracteres destinada a receber a sequência copiada de caracteres.

Observações

A origem e o destino podem sobrepor-se.

Exemplo

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

int main( )
{
    using namespace std;

    char_traits<char>::char_type sFrom1[] =  "abcd-1234-abcd";
    char_traits<char>::char_type sTo1[] =  "ABCD-1234";
    char_traits<char>::char_type* result1;
    cout << "The source string sFrom1 is: " << sFrom1 << endl;
    cout << "The destination stringsTo1 is: " << sTo1 << endl;
    result1 = char_traits<char>::_Move_s(sTo1,
        char_traits<char>::length(sTo1), sFrom1, 4);
    cout << "The result1 = _Move_s(sTo1, "
         << "char_traits<char>::length(sTo1), sFrom1, 4) is: "
         << result1 << endl << endl;

    // When source and destination overlap
    char_traits<char>::char_type sToFrom2[] = "abcd-1234-ABCD";
    char_traits<char>::char_type* result2;
    cout << "The source/destination string sToFrom2 is: "
         << sToFrom2 << endl;
    const char* findc = char_traits<char>::find(sToFrom2, 4, 'c');
    result2 = char_traits<char>::_Move_s(sToFrom2,
        char_traits<char>::length(sToFrom2), findc, 8);
    cout << "The result2 = _Move_s(sToFrom2, "
        << "char_traits<char>::length(sToFrom2), findc, 8) is: "
         << result2 << endl;
}
The source string sFrom1 is: abcd-1234-abcd
The destination stringsTo1 is: ABCD-1234
The result1 = _Move_s(sTo1, char_traits<char>::length(sTo1), sFrom1, 4) is: abcd-1234

The source/destination string sToFrom2 is: abcd-1234-ABCD
The result2 = _Move_s(sToFrom2, char_traits<char>::length(sToFrom2), findc, 8) is: cd-1234-4-ABCD

char_traits::not_eof

Testa se um caractere não é o caractere de fim de arquivo (EOF) ou é o EOF.

static int_type not_eof(const int_type& _Ch);

Parâmetros

_Ch
O caractere representado como um int_type a ser testado para saber se é o caractere EOF ou não.

Valor de retorno

A int_type representação do caráter testado, se o int_type do caráter não for igual ao do caráter EOF.

Se o valor do caractere int_type for igual ao valor EOF int_type , então false.

Exemplo

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

int main( ) {
   using namespace std;

   char_traits<char>::char_type ch1 =  'x';
   char_traits<char>::int_type int1;
   int1 = char_traits<char>:: to_int_type ( ch1 );
   cout << "The char_type ch1 is " << ch1
        << " corresponding to int_type: "
        << int1 << "." << endl;

   // EOF member function
   char_traits <char>::int_type int2 = char_traits<char>::eof ( );
   cout << "The eofReturn is: " << int2 << endl;

   // Testing for EOF or another character
   char_traits <char>::int_type eofTest1, eofTest2;
   eofTest1 = char_traits<char>::not_eof ( int1 );
   if ( !eofTest1 )
      cout << "The eofTest1 indicates ch1 is an EOF character."
              << endl;
   else
      cout << "The eofTest1 returns: " << eofTest1
           << ", which is the character: "
           <<  char_traits<char>::to_char_type ( eofTest1 )
           << "." << endl;

   eofTest2 = char_traits<char>::not_eof ( int2 );
   if ( !eofTest2 )
      cout << "The eofTest2 indicates int2 is an EOF character."
           << endl;
   else
      cout << "The eofTest1 returns: " << eofTest2
           << ", which is the character: "
           <<  char_traits<char>::to_char_type ( eofTest2 )
           << "." << endl;
}
The char_type ch1 is x corresponding to int_type: 120.
The eofReturn is: -1
The eofTest1 returns: 120, which is the character: x.
The eofTest2 indicates int2 is an EOF character.

char_traits::off_type

Um tipo inteiro que pode representar deslocamentos entre posições em um fluxo.

typedef streamoff off_type;

Observações

O tipo é um inteiro assinado que descreve um objeto que pode armazenar um deslocamento de byte envolvido em várias operações de posicionamento de fluxo. É tipicamente um sinônimo de streamoff, mas tem essencialmente as mesmas propriedades que esse tipo.

char_traits::p os_type

Um tipo inteiro que pode representar posições em um fluxo.

typedef streampos pos_type;

Observações

O tipo descreve um objeto que pode armazenar todas as informações necessárias para restaurar um indicador de posição de arquivo arbitrário dentro de um fluxo. É tipicamente um sinónimo de streampos, mas em qualquer caso tem essencialmente as mesmas propriedades que esse tipo.

char_traits::state_type

Um tipo que representa o estado de conversão para caracteres multibyte em um fluxo.

typedef implementation-defined state_type;

Observações

O tipo descreve um objeto que pode representar um estado de conversão. É tipicamente sinónimo de mbstate_t, mas em qualquer caso tem essencialmente as mesmas propriedades que esse tipo.

char_traits::to_char_type

Converte um int_type caractere no caractere correspondente char_type e retorna o resultado.

static char_type to_char_type(const int_type& _Ch);

Parâmetros

_Ch
O int_type caractere a ser representado como um char_typearquivo .

Valor de retorno

O char_type caractere correspondente ao int_type personagem.

Um valor de _Ch que não pode ser representado como tal produz um resultado não especificado.

Observações

As operações de conversão to_int_type e to_char_type são inversas entre si, de modo que:

to_int_type(to_char_type(x)) == x

para qualquer int_typex e

to_char_type(to_int_type(x)) == x

para qualquer char_typex.

Exemplo

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

int main( )
{
   using namespace std;

   char_traits<char>::char_type ch1 =  'a';
   char_traits<char>::char_type ch2 =  'b';
   char_traits<char>::char_type ch3 =  'a';

   // Converting from char_type to int_type
   char_traits<char>::int_type int1, int2 , int3;
   int1 =char_traits<char>:: to_int_type ( ch1 );
   int2 =char_traits<char>:: to_int_type ( ch2 );
   int3 =char_traits<char>:: to_int_type ( ch3 );

   cout << "The char_types and corresponding int_types are:"
        << "\n    ch1 = " << ch1 << " corresponding to int1 = "
        << int1 << "."
        << "\n    ch2 = " << ch2 << " corresponding to int1 = "
        << int2 << "."
        << "\n    ch3 = " << ch3 << " corresponding to int1 = "
        << int3 << "." << endl << endl;

   // Converting from int_type back to char_type
   char_traits<char>::char_type rec_ch1;
   rec_ch1 = char_traits<char>:: to_char_type ( int1);
   char_traits<char>::char_type rec_ch2;
   rec_ch2 = char_traits<char>:: to_char_type ( int2);

   cout << "The recovered char_types and corresponding int_types are:"
        << "\n    recovered ch1 = " << rec_ch1 << " from int1 = "
        << int1 << "."
        << "\n    recovered ch2 = " << rec_ch2 << " from int2 = "
        << int2 << "." << endl << endl;

   // Testing that the conversions are inverse operations
   bool b1 = char_traits<char>::eq ( rec_ch1 , ch1 );
   if ( b1 )
      cout << "The recovered char_type of ch1"
           << " is equal to the original ch1." << endl;
   else
      cout << "The recovered char_type of ch1"
           << " is not equal to the original ch1." << endl;

   // An equivalent and alternatively test procedure
   if ( rec_ch2 == ch2 )
      cout << "The recovered char_type of ch2"
           << " is equal to the original ch2." << endl;
   else
      cout << "The recovered char_type of ch2"
           << " is not equal to the original ch2." << endl;
}
The char_types and corresponding int_types are:
    ch1 = a corresponding to int1 = 97.
    ch2 = b corresponding to int1 = 98.
    ch3 = a corresponding to int1 = 97.

The recovered char_types and corresponding int_types are:
    recovered ch1 = a from int1 = 97.
    recovered ch2 = b from int2 = 98.

The recovered char_type of ch1 is equal to the original ch1.
The recovered char_type of ch2 is equal to the original ch2.

char_traits::to_int_type

Converte um char_type caractere no caractere correspondente int_type e retorna o resultado.

static int_type to_int_type(const char_type& _Ch);

Parâmetros

_Ch
O char_type caractere a ser representado como um int_typearquivo .

Valor de retorno

O int_type caractere correspondente ao char_type personagem.

Observações

As operações to_int_type de conversão e to_char_type são inversas entre si, de modo que:

to_int_type(to_char_type(x)) == x

para qualquer int_typex, e

to_char_type(to_int_type(x)) == x

para qualquer char_typex.

Exemplo

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

int main( )
{
   using namespace std;
   char_traits<char>::char_type ch1 = 'a';
   char_traits<char>::char_type ch2 = 'b';
   char_traits<char>::char_type ch3 = 'a';

   // Converting from char_type to int_type
   char_traits<char>::int_type int1, int2 , int3;
   int1 =char_traits<char>:: to_int_type ( ch1 );
   int2 =char_traits<char>:: to_int_type ( ch2 );
   int3 =char_traits<char>:: to_int_type ( ch3 );

   cout << "The char_types and corresponding int_types are:"
        << "\n    ch1 = " << ch1 << " corresponding to int1 = "
        << int1 << "."
        << "\n    ch2 = " << ch2 << " corresponding to int1 = "
        << int2 << "."
        << "\n    ch3 = " << ch3 << " corresponding to int1 = "
        << int3 << "." << endl << endl;

   // Converting from int_type back to char_type
   char_traits<char>::char_type rec_ch1;
   rec_ch1 = char_traits<char>:: to_char_type ( int1);
   char_traits<char>::char_type rec_ch2;
   rec_ch2 = char_traits<char>:: to_char_type ( int2);

   cout << "The recovered char_types and corresponding int_types are:"
        << "\n    recovered ch1 = " << rec_ch1 << " from int1 = "
        << int1 << "."
        << "\n    recovered ch2 = " << rec_ch2 << " from int2 = "
        << int2 << "." << endl << endl;

   // Testing that the conversions are inverse operations
   bool b1 = char_traits<char>::eq ( rec_ch1 , ch1 );
   if ( b1 )
      cout << "The recovered char_type of ch1"
           << " is equal to the original ch1." << endl;
   else
      cout << "The recovered char_type of ch1"
           << " is not equal to the original ch1." << endl;

   // An equivalent and alternatively test procedure
   if ( rec_ch2 == ch2 )
      cout << "The recovered char_type of ch2"
           << " is equal to the original ch2." << endl;
   else
      cout << "The recovered char_type of ch2"
           << " is not equal to the original ch2." << endl;
}
The char_types and corresponding int_types are:
    ch1 = a corresponding to int1 = 97.
    ch2 = b corresponding to int1 = 98.
    ch3 = a corresponding to int1 = 97.

The recovered char_types and corresponding int_types are:
    recovered ch1 = a from int1 = 97.
    recovered ch2 = b from int2 = 98.

The recovered char_type of ch1 is equal to the original ch1.
The recovered char_type of ch2 is equal to the original ch2.

Ver também

segurança de threads na biblioteca padrão C++