char_traits::assign
指定的字元值與另一個或對某個範圍的資料。
static void assign(
char_type& _CharTo,
const char_type& _CharFrom
);
static char_type *assign(
char_type* _StrTo,
size_t _Num,
char_type _CharFrom
);
參數
**_**CharFrom
值是指定的字元。_CharTo
要指派字元值的項目。_StrTo
初始項目是指定字元的字串或字元陣列值。_Num
為指定值的元素數目。
傳回值
第二 + 成成員函式指標傳回至第一個 _Num 項目是 _CharFrom 的指派值的字串。
範例
// 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;
}
需求
標頭:<string>
命名空間: std