Delen via


char_traits Struct

In de char_traits-struct worden kenmerken beschreven die aan een teken zijn gekoppeld.

Syntaxis

template <class CharType>
struct char_traits;

Parameterwaarden

CharType
Het gegevenstype element.

Opmerkingen

In de sjabloonstruct worden verschillende tekeneigenschappen voor het type CharTypebeschreven. De klassesjabloon basic_string en verschillende iostream-klassesjablonen, waaronder basic_ios, gebruiken deze informatie om elementen van het type CharTypete bewerken. Een dergelijk elementtype mag geen expliciete constructie of vernietiging vereisen. Er moet een standaardconstructor, een kopieerconstructor en een toewijzingsoperator worden geleverd met de verwachte semantiek. Een bitsgewijze kopie moet hetzelfde effect hebben als een opdracht. Geen van de lidfuncties van struct char_traits kan uitzonderingen genereren.

Typedefs

Typenaam Beschrijving
char_type Een type teken.
int_type Een geheel getal dat een teken van het type char_type of een EOF-teken (end-of-file) kan vertegenwoordigen.
off_type Een geheel getal dat verschuivingen tussen posities in een stroom kan vertegenwoordigen.
pos_type Een geheel getal dat posities in een stroom kan vertegenwoordigen.
state_type Een type dat de conversiestatus vertegenwoordigt voor meerderebyte tekens in een stroom.

Functies voor leden

Lid, functie Beschrijving
toewijzen Hiermee wijst u een tekenwaarde toe aan een ander teken.
vergelijken Vergelijkt maximaal een opgegeven aantal tekens in twee tekenreeksen.
kopiëren Hiermee kopieert u een opgegeven aantal tekens van de ene tekenreeks naar de andere. Afgeschreven Gebruik in plaats daarvan char_traits::_Copy_s .
_Copy_s Hiermee kopieert u een opgegeven aantal tekens van de ene tekenreeks naar de andere.
eof Retourneert het EOF-teken (end-of-file).
Eq Test of twee char_type tekens gelijk zijn.
eq_int_type Test of twee tekens die worden weergegeven als int_types gelijk zijn.
vinden Hiermee wordt gezocht naar het eerste exemplaar van een opgegeven teken in een reeks tekens.
lengte Retourneert de lengte van een tekenreeks.
Lt Test of het ene teken kleiner is dan een ander teken.
verplaatsen Kopieert een opgegeven aantal tekens in een reeks naar een andere, mogelijke overlappende reeks. Afgeschreven Gebruik in plaats daarvan char_traits::_Move_s .
_Move_s Kopieert een opgegeven aantal tekens in een reeks naar een andere, mogelijke overlappende reeks.
not_eof Test of een teken het EOF-teken (end-of-file) is.
to_char_type Converteert een int_type teken naar het bijbehorende char_type teken en retourneert het resultaat.
to_int_type Converteert een char_type teken naar het bijbehorende int_type teken en retourneert het resultaat.

Behoeften

Rubriek:<snaar>

naamruimte: std

char_traits::assign

Hiermee wijst u een tekenwaarde toe aan een andere tekenreeks of aan een bereik van elementen in een tekenreeks.

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

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

Parameterwaarden

_CharFrom Het teken waarvan de waarde moet worden toegewezen.

_CharTo
Het element waaraan de tekenwaarde moet worden toegewezen.

strTo
De tekenreeks of tekenmatrix waarvan de oorspronkelijke elementen moeten worden toegewezen.

_Num
Het aantal elementen waaraan waarden worden toegewezen.

Retourwaarde

De tweede lidfunctie retourneert een aanwijzer naar de tekenreeks waarvan de eerste _Num elementen zijn toegewezen aan _CharFrom.

Voorbeeld

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

Een type teken.

typedef CharType char_type;

Opmerkingen

Het type is een synoniem voor de sjabloonparameter CharType.

Voorbeeld

Zie het voorbeeld voor het kopiëren voor een voorbeeld van hoe u declareert en gebruikt char_type.

char_traits::compare

Vergelijkt maximaal een opgegeven aantal tekens in twee tekenreeksen.

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

Parameterwaarden

str1
De eerste van twee tekenreeksen die moeten worden vergeleken met elkaar.

str2
De tweede van twee tekenreeksen die moeten worden vergeleken met elkaar.

_Num
Het aantal elementen in de tekenreeksen dat moet worden vergeleken.

Retourwaarde

Een negatieve waarde als de eerste tekenreeks kleiner is dan de tweede tekenreeks, 0 als de twee tekenreeksen gelijk zijn of een positieve waarde als de eerste tekenreeks groter is dan de tweede tekenreeks.

Opmerkingen

De vergelijking tussen de tekenreeksen wordt gemaakt per element, eerst testen op gelijkheid en vervolgens, als een paar elementen in de reekstests niet gelijk zijn, worden ze getest op minder dan.

Als twee tekenreeksen gelijk zijn aan een bereik, maar één langer is dan de andere, is de kortere van de twee minder dan die van de twee.

Voorbeeld

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

Hiermee kopieert u een opgegeven aantal tekens van de ene tekenreeks naar de andere.

Deze methode is mogelijk onveilig, omdat deze afhankelijk is van de aanroeper om te controleren of de doorgegeven waarden juist zijn. Overweeg in plaats daarvan char_traits::_Copy_s te gebruiken.

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

Parameterwaarden

_Aan
Het element aan het begin van de tekenreeks of tekenmatrix die is gericht op het ontvangen van de gekopieerde reeks tekens.

_Van
Het element aan het begin van de brontekenreeks of tekenmatrix die moet worden gekopieerd.

_Num
Het aantal elementen dat moet worden gekopieerd.

Retourwaarde

Het eerste element dat is gekopieerd naar de tekenreeks of tekenmatrix die is gericht op het ontvangen van de gekopieerde reeks tekens.

Opmerkingen

De bron- en doeltekenreeksen mogen niet overlappen.

Voorbeeld

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

Hiermee kopieert u een opgegeven aantal tekens van de ene tekenreeks naar de andere.

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

Parameterwaarden

dest
De tekenreeks of tekenmatrix die is gericht op het ontvangen van de gekopieerde reeks tekens.

dest_size
De grootte van dest. Als char_type dat het is char, is deze grootte in bytes. Als char_type dat het is wchar_t, is deze grootte in woorden.

_Van
De brontekenreeks of tekenmatrix die moet worden gekopieerd.

aantal
Het aantal elementen dat moet worden gekopieerd.

Retourwaarde

De tekenreeks of tekenmatrix die is gericht op het ontvangen van de gekopieerde reeks tekens.

Opmerkingen

De bron- en doeltekenreeksen mogen niet overlappen.

Voorbeeld

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

Retourneert het EOF-teken (end-of-file).

static int_type eof();

Retourwaarde

Het EOF-teken.

Opmerkingen

Een waarde die het einde van het bestand aangeeft (zoals EOF of WEOF).

De C++-standaard geeft aan dat deze waarde niet mag overeenkomen met een geldige char_type waarde. De Microsoft C++-compiler dwingt deze beperking af voor het type char, maar niet voor het type wchar_t. In het onderstaande voorbeeld ziet u dit.

Voorbeeld

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

Test of twee char_type tekens gelijk zijn.

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

Parameterwaarden

_Ch1
De eerste van twee tekens die moeten worden getest op gelijkheid.

_Ch2
De tweede van twee tekens die moeten worden getest op gelijkheid.

Retourwaarde

true als het eerste teken gelijk is aan het tweede teken; anders false.

Voorbeeld

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

Test of twee tekens die worden weergegeven als int_types gelijk zijn of niet.

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

Parameterwaarden

_Ch1
De eerste van de twee tekens die moeten worden getest op gelijkheid als int_types.

_Ch2
De tweede van de twee tekens die moeten worden getest op gelijkheid als int_types.

Retourwaarde

true als het eerste teken gelijk is aan het tweede teken; anders false.

Voorbeeld

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

Hiermee wordt gezocht naar het eerste exemplaar van een opgegeven teken in een reeks tekens.

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

Parameterwaarden

Str
Het eerste teken in de tekenreeks dat moet worden doorzocht.

_Num
Het aantal posities, tellend van de eerste, in het bereik dat moet worden doorzocht.

_Ch
Het teken dat moet worden gezocht in het bereik.

Retourwaarde

Een aanwijzer naar het eerste exemplaar van het opgegeven teken in het bereik als er een overeenkomst wordt gevonden; anders een null-aanwijzer.

Voorbeeld

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

Een geheel getal dat een teken van het type char_type of een EOF-teken (end-of-file) kan vertegenwoordigen.

typedef long int_type;

Opmerkingen

Het moet mogelijk zijn om een waarde van het type CharType te typen die int_type vervolgens moet worden teruggezet CharType zonder de oorspronkelijke waarde te wijzigen.

Voorbeeld

Zie het voorbeeld voor eq_int_type voor een voorbeeld van het declareren en gebruiken int_typevan .

char_traits::length

Retourneert de lengte van een tekenreeks.

static size_t length(const char_type* str);

Parameterwaarden

Str
De C-tekenreeks waarvan de lengte moet worden gemeten.

Retourwaarde

Het aantal elementen in de reeks dat wordt gemeten, niet inclusief het null-eindteken.

Voorbeeld

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

Test of het ene teken kleiner is dan een ander teken.

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

Parameterwaarden

_Ch1
De eerste van twee tekens die moeten worden getest op minder dan.

_Ch2
De tweede van twee tekens die moeten worden getest op minder dan.

Retourwaarde

true als het eerste teken kleiner is dan het tweede teken; anders false.

Voorbeeld

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

Kopieert een opgegeven aantal tekens in een reeks naar een andere, mogelijk overlappende reeks.

Deze methode is mogelijk onveilig, omdat deze afhankelijk is van de aanroeper om te controleren of de doorgegeven waarden juist zijn. Overweeg in plaats daarvan char_traits::_Move_s te gebruiken.

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

Parameterwaarden

_Aan
Het element aan het begin van de tekenreeks of tekenmatrix die is gericht op het ontvangen van de gekopieerde reeks tekens.

_Van
Het element aan het begin van de brontekenreeks of tekenmatrix die moet worden gekopieerd.

_Num
Het aantal elementen dat moet worden gekopieerd uit de brontekenreeks.

Retourwaarde

Het eerste element _To gekopieerd naar de tekenreeks of tekenmatrix die is gericht op het ontvangen van de gekopieerde reeks tekens.

Opmerkingen

De bron en het doel kunnen elkaar overlappen.

Voorbeeld

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

Kopieert een opgegeven aantal tekens in een reeks naar een andere, mogelijk overlappende reeks.

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

Parameterwaarden

dest
Het element aan het begin van de tekenreeks of tekenmatrix die is gericht op het ontvangen van de gekopieerde reeks tekens.

dest_size
De grootte van dest. Als char_type dat het is char, is dit in bytes. Als char_type dat het is wchar_t, is dit in woorden.

_Van
Het element aan het begin van de brontekenreeks of tekenmatrix die moet worden gekopieerd.

aantal
Het aantal elementen dat moet worden gekopieerd uit de brontekenreeks.

Retourwaarde

Het eerste element dat is gekopieerd naar de tekenreeks of tekenmatrix die is gericht op het ontvangen van de gekopieerde reeks tekens.

Opmerkingen

De bron en het doel kunnen elkaar overlappen.

Voorbeeld

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

Test of een teken niet het EOF-teken (end-of-file) is of het EOF is.

static int_type not_eof(const int_type& _Ch);

Parameterwaarden

_Ch
Het teken dat wordt weergegeven als int_type een teken dat moet worden getest of het het EOF-teken is of niet.

Retourwaarde

De int_type weergave van het geteste teken, als het int_type teken niet gelijk is aan dat van het EOF-teken.

Als de tekenwaarde int_type gelijk is aan de EOF-waarde int_type , dan false.

Voorbeeld

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

Een geheel getal dat verschuivingen tussen posities in een stroom kan vertegenwoordigen.

typedef streamoff off_type;

Opmerkingen

Het type is een ondertekend geheel getal dat een object beschrijft dat een byte-offset kan opslaan die betrokken is bij verschillende bewerkingen voor het positioneren van stromen. Het is meestal een synoniem voor streamoff, maar het heeft in wezen dezelfde eigenschappen als dat type.

char_traits::p os_type

Een geheel getal dat posities in een stroom kan vertegenwoordigen.

typedef streampos pos_type;

Opmerkingen

Het type beschrijft een object dat alle informatie kan opslaan die nodig is om een willekeurige indicator voor bestandspositie in een stroom te herstellen. Het is meestal een synoniem voor streampos, maar in elk geval heeft het dezelfde eigenschappen als dat type.

char_traits::state_type

Een type dat de conversiestatus vertegenwoordigt voor meerderebyte tekens in een stroom.

typedef implementation-defined state_type;

Opmerkingen

Het type beschrijft een object dat een conversiestatus kan vertegenwoordigen. Het is meestal een synoniem voor mbstate_t, maar in elk geval heeft het in feite dezelfde eigenschappen als dat type.

char_traits::to_char_type

Converteert een int_type teken naar het bijbehorende char_type teken en retourneert het resultaat.

static char_type to_char_type(const int_type& _Ch);

Parameterwaarden

_Ch
Het int_type teken dat moet worden weergegeven als een char_type.

Retourwaarde

Het char_type teken dat overeenkomt met het int_type teken.

Een waarde van _Ch die niet als zodanig kan worden weergegeven, resulteert in een niet-opgegeven resultaat.

Opmerkingen

De conversiebewerkingen to_int_type en to_char_type zijn omgekeerd met elkaar, zodat:

to_int_type(to_char_type(x)) == x

voor elke int_typex en

to_char_type(to_int_type(x)) == x

voor elke char_typex.

Voorbeeld

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

Converteert een char_type teken naar het bijbehorende int_type teken en retourneert het resultaat.

static int_type to_int_type(const char_type& _Ch);

Parameterwaarden

_Ch
Het char_type teken dat moet worden weergegeven als een int_type.

Retourwaarde

Het int_type teken dat overeenkomt met het char_type teken.

Opmerkingen

De conversiebewerkingen to_int_type en to_char_type zijn omgekeerd van elkaar, zodat:

to_int_type(to_char_type(x)) == x

voor elke int_typex, en

to_char_type(to_int_type(x)) == x

voor elke char_typex.

Voorbeeld

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

Zie ook

Thread Safety in de standaardbibliotheek van C++