Udostępnij przez


char_traits — Struktura

Struktura char_traits opisuje atrybuty skojarzone z znakiem.

Składnia

template <class CharType>
struct char_traits;

Parametry

Chartype
Typ danych elementu.

Uwagi

Struktura szablonu opisuje różne cechy znaków dla typu CharType. Szablon klasy basic_string , a także kilka szablonów klas iostream, w tym basic_ios, użyj tych informacji do manipulowania elementami typu CharType. Taki typ elementu nie może wymagać wyraźnej konstrukcji ani zniszczenia. Musi podać konstruktor domyślny, konstruktor kopiujący i operator przypisania z oczekiwaną semantyczną. Kopia bitowa musi mieć taki sam efekt jak przypisanie. Żadna z funkcji składowych struktury char_traits może zgłaszać wyjątki.

Typedefs

Nazwa typu opis
Char_type Typ znaku.
Int_type Typ liczby całkowitej, który może reprezentować znak typu char_type lub znak końca pliku (EOF).
off_type Typ liczby całkowitej, który może reprezentować przesunięcia między pozycjami w strumieniu.
pos_type Typ liczby całkowitej, który może reprezentować pozycje w strumieniu.
state_type Typ reprezentujący stan konwersji dla znaków wielobajtowych w strumieniu.

Funkcje składowe

Funkcja składowa opis
Przypisać Przypisuje jedną wartość znaku do innej.
Porównać Porównuje się z określoną liczbą znaków w dwóch ciągach.
Kopii Kopiuje określoną liczbę znaków z jednego ciągu do innego. Przestarzałe. Zamiast tego użyj char_traits::_Copy_s .
_Copy_s Kopiuje określoną liczbę znaków z jednego ciągu do innego.
eof Zwraca znak końca pliku (EOF).
Eq Sprawdza, czy dwa char_type znaki są równe.
eq_int_type Sprawdza, czy dwa znaki reprezentowane jako int_types są równe.
find Wyszukuje pierwsze wystąpienie określonego znaku w zakresie znaków.
Długość Zwraca długość ciągu.
Por Sprawdza, czy jeden znak jest mniejszy niż inny.
Przenieść Kopiuje określoną liczbę znaków w sekwencji do innej, możliwej nakładania się, sekwencji. Przestarzałe. Zamiast tego użyj char_traits::_Move_s .
_Move_s Kopiuje określoną liczbę znaków w sekwencji do innej, możliwej nakładania się, sekwencji.
not_eof Sprawdza, czy znak jest znakiem końca pliku (EOF).
to_char_type Konwertuje int_type znak na odpowiedni char_type znak i zwraca wynik.
to_int_type Konwertuje char_type znak na odpowiedni int_type znak i zwraca wynik.

Wymagania

Nagłówek:<ciąg>

Przestrzeń nazw: std

char_traits::assign

Przypisuje jedną wartość znaku do innego lub do zakresu elementów w ciągu.

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

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

Parametry

_CharFrom Znak, którego wartość ma zostać przypisana.

_CharTo
Element, który ma zostać przypisany do wartości znaku.

strTo
Ciąg lub tablica znaków, której początkowe elementy mają być przypisane wartości znaków.

_Num
Liczba elementów, które zostaną przypisane wartości.

Wartość zwracana

Druga funkcja składowa zwraca wskaźnik do ciągu, którego pierwsze _Num elementy zostały przypisane wartości _CharFrom.

Przykład

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

Typ znaku.

typedef CharType char_type;

Uwagi

Typ jest synonimem parametru CharTypeszablonu .

Przykład

Zobacz przykład kopiowania, aby zapoznać się z przykładem sposobu deklarowania i używania elementu char_type.

char_traits::compare

Porównuje się z określoną liczbą znaków w dwóch ciągach.

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

Parametry

str1
Pierwszy z dwóch ciągów do porównania ze sobą.

str2
Drugi z dwóch ciągów do porównania ze sobą.

_Num
Liczba elementów w ciągach do porównania.

Wartość zwracana

Wartość ujemna, jeśli pierwszy ciąg jest mniejszy niż drugi ciąg, 0, jeśli dwa ciągi są równe, lub wartość dodatnia, jeśli pierwszy ciąg jest większy niż drugi ciąg.

Uwagi

Porównanie ciągów jest wykonywane według elementu, najpierw testowanie równości, a następnie, jeśli para elementów w testach sekwencji nie jest równa, są one testowane pod kątem mniejszej niż.

Jeśli dwa ciągi są porównywane równe w zakresie, ale jeden jest dłuższy niż drugi, krótszy z dwóch jest mniejszy niż dłuższy.

Przykład

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

Kopiuje określoną liczbę znaków z jednego ciągu do innego.

Ta metoda jest potencjalnie niebezpieczna, ponieważ opiera się na obiekcie wywołującym, aby sprawdzić, czy przekazane wartości są poprawne. Rozważ użycie char_traits ::_Copy_s .

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

Parametry

_To
Element na początku ciągu lub tablicy znaków przeznaczony do odbierania skopiowanej sekwencji znaków.

_Z
Element na początku ciągu źródłowego lub tablicy znaków do skopiowania.

_Num
Liczba elementów do skopiowania.

Wartość zwracana

Pierwszy element skopiowany do ciągu lub tablicy znaków przeznaczony do odbierania skopiowanej sekwencji znaków.

Uwagi

Sekwencje znaków źródłowych i docelowych nie mogą się nakładać.

Przykład

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

Kopiuje określoną liczbę znaków z jednego ciągu do innego.

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

Parametry

Dest
Ciąg lub tablica znaków przeznaczona do odbierania skopiowanej sekwencji znaków.

dest_size
Rozmiar dest. Jeśli char_type ma wartość char, ten rozmiar jest wyrażony w bajtach. Jeśli char_type ma wartość wchar_t, ten rozmiar jest wyrażony w słowach.

_Z
Ciąg źródłowy lub tablica znaków do skopiowania.

count
Liczba elementów do skopiowania.

Wartość zwracana

Ciąg lub tablica znaków przeznaczona do odbierania skopiowanej sekwencji znaków.

Uwagi

Sekwencje znaków źródłowych i docelowych nie mogą się nakładać.

Przykład

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

Zwraca znak końca pliku (EOF).

static int_type eof();

Wartość zwracana

Znak EOF.

Uwagi

Wartość reprezentująca koniec pliku (np. EOF lub WEOF).

Standard C++ stwierdza, że ta wartość nie może odpowiadać prawidłowej char_type wartości. Kompilator języka Microsoft C++ wymusza to ograniczenie dla typu char, ale nie dla typu wchar_t. Prezentuje to poniższy przykład.

Przykład

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

Sprawdza, czy dwa char_type znaki są równe.

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

Parametry

_Ch1
Pierwszy z dwóch znaków do przetestowania pod kątem równości.

_Ch2
Drugi z dwóch znaków do przetestowania pod kątem równości.

Wartość zwracana

true jeśli pierwszy znak jest równy drugiemu znakowi; w przeciwnym razie false.

Przykład

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

Sprawdza, czy dwa znaki reprezentowane jako int_types są równe, czy nie.

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

Parametry

_Ch1
Pierwszy z dwóch znaków do przetestowania pod kątem równości jako int_types.

_Ch2
Drugi z dwóch znaków do przetestowania pod kątem równości jako int_types.

Wartość zwracana

true jeśli pierwszy znak jest równy drugiemu znakowi; w przeciwnym razie false.

Przykład

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

Wyszukuje pierwsze wystąpienie określonego znaku w zakresie znaków.

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

Parametry

Str
Pierwszy znak w ciągu do wyszukania.

_Num
Liczba pozycji, licząc od pierwszego, w zakresie do wyszukania.

_Ch
Znak, który ma być wyszukiwany w zakresie.

Wartość zwracana

Wskaźnik do pierwszego wystąpienia określonego znaku w zakresie, jeśli zostanie znalezione dopasowanie; w przeciwnym razie wskaźnik o wartości null.

Przykład

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

Typ liczby całkowitej, który może reprezentować znak typu char_type lub znak końca pliku (EOF).

typedef long int_type;

Uwagi

Należy wpisać rzutowanie wartości typu CharType , aby int_type następnie wrócić do CharType bez zmiany oryginalnej wartości.

Przykład

Zobacz przykład eq_int_type, aby zapoznać się z przykładem sposobu deklarowania i używania elementu int_type.

char_traits::length

Zwraca długość ciągu.

static size_t length(const char_type* str);

Parametry

Str
Ciąg C, którego długość ma być mierzona.

Wartość zwracana

Liczba elementów mierzonych w sekwencji, a nie w tym terminator o wartości null.

Przykład

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

Sprawdza, czy jeden znak jest mniejszy niż inny.

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

Parametry

_Ch1
Pierwszy z dwóch znaków do przetestowania pod kątem mniejszej liczby znaków.

_Ch2
Drugi z dwóch znaków do przetestowania pod kątem mniejszej liczby znaków.

Wartość zwracana

true jeśli pierwszy znak jest mniejszy niż drugi znak; w przeciwnym razie false.

Przykład

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

Kopiuje określoną liczbę znaków w sekwencji do innej, prawdopodobnie nakładających się sekwencji.

Ta metoda jest potencjalnie niebezpieczna, ponieważ opiera się na obiekcie wywołującym, aby sprawdzić, czy przekazane wartości są poprawne. Rozważ użycie char_traits::_Move_s .

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

Parametry

_To
Element na początku ciągu lub tablicy znaków przeznaczony do odbierania skopiowanej sekwencji znaków.

_Z
Element na początku ciągu źródłowego lub tablicy znaków do skopiowania.

_Num
Liczba elementów do skopiowania z ciągu źródłowego.

Wartość zwracana

Pierwszy element _To skopiowany do tablicy ciągów lub znaków przeznaczony do odbierania skopiowanej sekwencji znaków.

Uwagi

Źródło i miejsce docelowe mogą się nakładać.

Przykład

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

Kopiuje określoną liczbę znaków w sekwencji do innej, prawdopodobnie nakładających się sekwencji.

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

Parametry

Dest
Element na początku ciągu lub tablicy znaków przeznaczony do odbierania skopiowanej sekwencji znaków.

dest_size
Rozmiar dest. Jeśli char_type wartość to char, jest to w bajtach. Jeśli char_type ma wartość wchar_t, to jest to wyrazy.

_Z
Element na początku ciągu źródłowego lub tablicy znaków do skopiowania.

count
Liczba elementów do skopiowania z ciągu źródłowego.

Wartość zwracana

Pierwszy element został skopiowany do ciągu lub tablicy znaków przeznaczonej do odbierania skopiowanej sekwencji znaków.

Uwagi

Źródło i miejsce docelowe mogą się nakładać.

Przykład

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

Sprawdza, czy znak nie jest znakiem końca pliku (EOF), czy też znakiem EOF.

static int_type not_eof(const int_type& _Ch);

Parametry

_Ch
Znak reprezentowany jako element do przetestowania pod kątem int_type tego, czy jest to znak EOF, czy nie.

Wartość zwracana

Reprezentacja int_type przetestowanego znaku, jeśli int_type znak nie jest równy znakowi EOF.

Jeśli wartość znaku int_type jest równa wartości EOF int_type , to false.

Przykład

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

Typ liczby całkowitej, który może reprezentować przesunięcia między pozycjami w strumieniu.

typedef streamoff off_type;

Uwagi

Typ to podpisana liczba całkowita, która opisuje obiekt, który może przechowywać przesunięcie bajtów związane z różnymi operacjami pozycjonowania strumienia. Zazwyczaj jest to synonim streamoff, ale zasadniczo ma te same właściwości co ten typ.

char_traits::p os_type

Typ liczby całkowitej, który może reprezentować pozycje w strumieniu.

typedef streampos pos_type;

Uwagi

Typ opisuje obiekt, który może przechowywać wszystkie informacje potrzebne do przywrócenia dowolnego wskaźnika położenia pliku w strumieniu. Zazwyczaj jest to synonim streampos, ale w każdym przypadku ma zasadniczo te same właściwości co ten typ.

char_traits::state_type

Typ reprezentujący stan konwersji dla znaków wielobajtowych w strumieniu.

typedef implementation-defined state_type;

Uwagi

Typ opisuje obiekt, który może reprezentować stan konwersji. Zazwyczaj jest to synonim , mbstate_tale w każdym przypadku ma zasadniczo te same właściwości co ten typ.

char_traits::to_char_type

Konwertuje int_type znak na odpowiedni char_type znak i zwraca wynik.

static char_type to_char_type(const int_type& _Ch);

Parametry

_Ch
Znak int_type , który ma być reprezentowany char_typejako .

Wartość zwracana

Znak char_type odpowiadający znakowi int_type .

Wartość _Ch , której nie można przedstawić jako takie, daje nieokreślony wynik.

Uwagi

Operacje konwersji to_int_type i to_char_type są odwrotne do siebie, tak aby:

to_int_type ( ( to_char_typex ) ) == x

dla dowolnego int_typex i

to_char_type ( ( to_int_typex ) ) == x

dla każdego char_typex.

Przykład

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

Konwertuje char_type znak na odpowiedni int_type znak i zwraca wynik.

static int_type to_int_type(const char_type& _Ch);

Parametry

_Ch
Znakchar_type, który ma być reprezentowany jako .int_type

Wartość zwracana

Znak int_type odpowiadający znakowi char_type .

Uwagi

Operacje to_int_type konwersji i to_char_type są odwrotne do siebie, tak aby:

to_int_type ( ( to_char_typex ) ) == x

dla każdego int_typex i

to_char_type ( ( to_int_typex ) ) == x

dla każdego char_typex.

Przykład

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

Zobacz też

Bezpieczeństwo wątku w standardowej bibliotece C++