Dela via


char_traits Struct

I char_traits struct beskrivs attribut som är associerade med ett tecken.

Syntax

template <class CharType>
struct char_traits;

Parameterar

CharType
Elementdatatypen.

Anmärkningar

Mallens struct beskriver olika karaktärsdrag för typen CharType. Klassmallen basic_string samt flera iostream-klassmallar, inklusive basic_ios, använder den här informationen för att ändra element av typen CharType. En sådan elementtyp får inte kräva explicit konstruktion eller förstörelse. Den måste ange en standardkonstruktor, en kopieringskonstruktor och en tilldelningsoperator med förväntade semantik. En bitvis kopia måste ha samma effekt som en tilldelning. Ingen av medlemsfunktionerna i struct char_traits kan utlösa undantag.

Typedefs

Typnamn Beskrivning
char_type En typ av tecken.
int_type En heltalstyp som kan representera ett tecken av typen char_type eller ett EOF-tecken (end-of-file).
off_type En heltalstyp som kan representera förskjutningar mellan positioner i en ström.
pos_type En heltalstyp som kan representera positioner i en ström.
state_type En typ som representerar konverteringstillståndet i för flerabytestecken i en dataström.

Medlemsfunktioner

Medlemsfunktion Beskrivning
tilldela Tilldelar ett teckenvärde till ett annat.
jämföra Jämför upp till ett angivet antal tecken i två strängar.
kopia Kopierar ett angivet antal tecken från en sträng till en annan. Avrådd. Använd char_traits::_Copy_s i stället.
_Copy_s Kopierar ett angivet antal tecken från en sträng till en annan.
Eof Returnerar EOF-tecknet (end-of-file).
Eq Testar om två char_type tecken är lika med.
eq_int_type Testar om två tecken som representeras som int_types är lika med.
hitta Söker efter den första förekomsten av ett angivet tecken i ett teckenintervall.
längd Returnerar längden på en sträng.
Lt Testar om ett tecken är mindre än ett annat.
flytta Kopierar ett angivet antal tecken i en sekvens till en annan, möjlig överlappning, sekvens. Avrådd. Använd char_traits::_Move_s i stället.
_Move_s Kopierar ett angivet antal tecken i en sekvens till en annan, möjlig överlappning, sekvens.
not_eof Testar om ett tecken är EOF-tecknet (end-of-file).
to_char_type Konverterar ett int_type tecken till motsvarande char_type tecken och returnerar resultatet.
to_int_type Konverterar ett char_type tecken till motsvarande int_type tecken och returnerar resultatet.

Kravspecifikation

Rubrik:<sträng>

namnområde: std

char_traits::assign

Tilldelar ett teckenvärde till ett annat eller till ett område med element i en sträng.

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

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

Parameterar

_CharFrom Tecknet vars värde ska tilldelas.

_CharTo
Elementet som ska tilldelas teckenvärdet.

strTo
Strängen eller teckenmatrisen vars inledande element ska tilldelas teckenvärden.

_Num
Antalet element som ska tilldelas värden.

Returvärde

Den andra medlemsfunktionen returnerar en pekare till strängen vars första _Num element har tilldelats värden för _CharFrom.

Exempel

// 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

En typ av tecken.

typedef CharType char_type;

Anmärkningar

Typen är en synonym för mallparametern CharType.

Exempel

Se exemplet för kopia för ett exempel på hur du deklarerar och använder char_type.

char_traits::compare

Jämför upp till ett angivet antal tecken i två strängar.

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

Parameterar

str1
Den första av två strängar som ska jämföras med varandra.

str2
Den andra av två strängar som ska jämföras med varandra.

_Num
Antalet element i strängarna som ska jämföras.

Returvärde

Ett negativt värde om den första strängen är mindre än den andra strängen, 0 om de två strängarna är lika med eller ett positivt värde om den första strängen är större än den andra strängen.

Anmärkningar

Jämförelsen mellan strängarna görs element för element, först testning för likhet och sedan, om ett par element i sekvenstesterna inte är lika med, testas de för mindre än.

Om två strängar jämför lika med ett intervall men det ena är längre än det andra, är den kortare av de två mindre än den längre.

Exempel

// 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::copy

Kopierar ett angivet antal tecken från en sträng till en annan.

Den här metoden är potentiellt osäker eftersom den förlitar sig på anroparen för att kontrollera att de anförda värdena är korrekta. Överväg att använda char_traits::_Copy_s i stället.

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

Parameterar

_Till
Elementet i början av strängen eller teckenmatrisen som ska ta emot den kopierade teckensekvensen.

_Från
Elementet i början av källsträngen eller teckenmatrisen som ska kopieras.

_Num
Antalet element som ska kopieras.

Returvärde

Det första elementet som kopieras till strängen eller teckenmatrisen som är mål för att ta emot den kopierade sekvensen med tecken.

Anmärkningar

Käll- och målteckensekvenserna får inte överlappa varandra.

Exempel

// 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

Kopierar ett angivet antal tecken från en sträng till en annan.

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

Parameterar

dest
Sträng- eller teckenmatrisen som ska ta emot den kopierade teckensekvensen.

dest_size
Storleken på dest. Om char_type är charär är den här storleken i byte. Om char_type är wchar_tär är den här storleken i ord.

_Från
Källsträngen eller teckenmatrisen som ska kopieras.

antal
Antalet element som ska kopieras.

Returvärde

Sträng- eller teckenmatrisen som ska ta emot den kopierade teckensekvensen.

Anmärkningar

Käll- och målteckensekvenserna får inte överlappa varandra.

Exempel

// 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

Returnerar EOF-tecknet (end-of-file).

static int_type eof();

Returvärde

EOF-tecknet.

Anmärkningar

Ett värde som representerar filslut (till exempel EOF eller WEOF).

C++-standarden anger att det här värdet inte får motsvara ett giltigt char_type värde. Microsoft C++-kompilatorn tillämpar den här begränsningen för typen char, men inte för typen wchar_t. Exemplet nedan visar detta.

Exempel

// 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

Testar om två char_type tecken är lika med.

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

Parameterar

_Ch1
Det första av två tecken som ska testas för likhet.

_Ch2
Det andra av två tecken som ska testas för likhet.

Returvärde

true om det första tecknet är lika med det andra tecknet; annars false.

Exempel

// 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

Testar om två tecken som representeras som int_types är lika med eller inte.

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

Parameterar

_Ch1
Det första av de två tecknen som ska testas för likhet som int_types.

_Ch2
Det andra av de två tecken som ska testas för likhet som int_types.

Returvärde

true om det första tecknet är lika med det andra tecknet; annars false.

Exempel

// 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::find

Söker efter den första förekomsten av ett angivet tecken i ett teckenintervall.

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

Parameterar

Str
Det första tecknet i strängen som ska sökas igenom.

_Num
Antalet positioner, som räknas från den första, i det intervall som ska sökas.

_Ch
Det tecken som ska sökas efter i intervallet.

Returvärde

En pekare till den första förekomsten av det angivna tecknet i intervallet om en matchning hittas. annars en null-pekare.

Exempel

// 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

En heltalstyp som kan representera ett tecken av typen char_type eller ett EOF-tecken (end-of-file).

typedef long int_type;

Anmärkningar

Det måste vara möjligt att skriva gjutna ett värde av typen CharType till int_type sedan tillbaka till CharType utan att ändra det ursprungliga värdet.

Exempel

Se exemplet för eq_int_type för ett exempel på hur du deklarerar och använder int_type.

char_traits::length

Returnerar längden på en sträng.

static size_t length(const char_type* str);

Parameterar

Str
C-strängen vars längd ska mätas.

Returvärde

Antalet element i sekvensen som mäts, inte null-avslutaren.

Exempel

// 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

Testar om ett tecken är mindre än ett annat.

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

Parameterar

_Ch1
Det första av två tecken som ska testas för mindre än.

_Ch2
Det andra av två tecken som ska testas för mindre än.

Returvärde

true om det första tecknet är mindre än det andra tecknet; annars false.

Exempel

// 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

Kopierar ett angivet antal tecken i en sekvens till en annan, eventuellt överlappande sekvens.

Den här metoden är potentiellt osäker eftersom den förlitar sig på anroparen för att kontrollera att de anförda värdena är korrekta. Överväg att använda char_traits::_Move_s i stället.

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

Parameterar

_Till
Elementet i början av strängen eller teckenmatrisen som ska ta emot den kopierade teckensekvensen.

_Från
Elementet i början av källsträngen eller teckenmatrisen som ska kopieras.

_Num
Antalet element som ska kopieras från källsträngen.

Returvärde

Det första elementet _To kopierats till strängen eller teckenmatrisen som mål för att ta emot den kopierade teckensekvensen.

Anmärkningar

Källan och målet kan överlappa varandra.

Exempel

// 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

Kopierar ett angivet antal tecken i en sekvens till en annan, eventuellt överlappande sekvens.

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

Parameterar

dest
Elementet i början av strängen eller teckenmatrisen som ska ta emot den kopierade teckensekvensen.

dest_size
Storleken på dest. Om char_type är char, är detta i byte. Om char_type är wchar_t, är detta i ord.

_Från
Elementet i början av källsträngen eller teckenmatrisen som ska kopieras.

antal
Antalet element som ska kopieras från källsträngen.

Returvärde

Det första elementet kopieras till sträng- eller teckenmatrisen för att ta emot den kopierade sekvensen med tecken.

Anmärkningar

Källan och målet kan överlappa varandra.

Exempel

// 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

Testar om ett tecken inte är EOF-tecknet (end-of-file) eller är EOF.

static int_type not_eof(const int_type& _Ch);

Parameterar

_Ch
Tecknet som representeras som ett int_type som ska testas för om det är EOF-tecknet eller inte.

Returvärde

Representationen int_type av det testade tecknet, om int_type tecknet inte är lika med EOF-tecknet.

Om teckenvärdet int_type är lika med EOF-värdet int_type , så false.

Exempel

// 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

En heltalstyp som kan representera förskjutningar mellan positioner i en ström.

typedef streamoff off_type;

Anmärkningar

Typen är ett signerat heltal som beskriver ett objekt som kan lagra en byteförskjutning som ingår i olika strömpositioneringsåtgärder. Det är vanligtvis en synonym för utströmning, men den har i stort sett samma egenskaper som den typen.

char_traits::p os_type

En heltalstyp som kan representera positioner i en ström.

typedef streampos pos_type;

Anmärkningar

Typen beskriver ett objekt som kan lagra all information som behövs för att återställa en godtycklig filpositionsindikator i en ström. Det är vanligtvis en synonym för streampos, men i vilket fall som helst har den i stort sett samma egenskaper som den typen.

char_traits::state_type

En typ som representerar konverteringstillståndet för flerabytestecken i en ström.

typedef implementation-defined state_type;

Anmärkningar

Typen beskriver ett objekt som kan representera ett konverteringstillstånd. Det är vanligtvis en synonym för mbstate_t, men i vilket fall som helst har den i stort sett samma egenskaper som den typen.

char_traits::to_char_type

Konverterar ett int_type tecken till motsvarande char_type tecken och returnerar resultatet.

static char_type to_char_type(const int_type& _Ch);

Parameterar

_Ch
Tecknet int_type som ska representeras som en char_type.

Returvärde

Tecknet char_type som motsvarar int_type tecknet.

Ett värde på _Ch som inte kan representeras som sådant ger ett ospecificerat resultat.

Anmärkningar

Konverteringsåtgärderna to_int_type och to_char_type är omvända mot varandra, så att:

to_int_type(to_char_type(x)) == x

för alla int_typex och

to_char_type(to_int_type(x)) == x

för alla char_typex.

Exempel

// 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

Konverterar ett char_type tecken till motsvarande int_type tecken och returnerar resultatet.

static int_type to_int_type(const char_type& _Ch);

Parameterar

_Ch
Tecknet char_type som ska representeras som en int_type.

Returvärde

Tecknet int_type som motsvarar char_type tecknet.

Anmärkningar

Konverteringsåtgärderna to_int_type och to_char_type är omvända mot varandra, så att:

to_int_type(to_char_type(x)) == x

för alla int_typex och

to_char_type(to_int_type(x)) == x

för alla char_typex.

Exempel

// 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.

Se även

Trådsäkerhet i C++ Standard Library