Partager via


char_traits::assign

Assigns one character value to another or to a range of elements in a string.

static void assign(
   char_type& _CharTo, 
   const char_type& _CharFrom 
);
static char_type *assign(
   char_type* _StrTo, 
   size_t _Num, 
   char_type _CharFrom 
);

Parameters

  • **_**CharFrom
    The character whose value is to be assigned.

  • _CharTo
    The element that is to be assigned the character value.

  • _StrTo
    The string or character array whose initial elements are to be assigned character values.

  • _Num
    The number of elements that are going to be assigned values.

Return Value

The second member function returns a pointer to the string whose first _Num elements have been assigned values of _CharFrom.

Example

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

Requirements

Header: <string>

Namespace: std

See Also

Concepts

char_traits Struct

char_traits Members