basic_string::operator=
将新的字符值为字符串的内容。
basic_string<CharType, Traits, Allocator>& operator=(
value_type _Ch
);
basic_string<CharType, Traits, Allocator>& operator=(
const value_type* _Ptr
);
basic_string<CharType, Traits, Allocator>& operator=(
const basic_string<CharType, Traits, Allocator>& _Right
);
basic_string<CharType, Traits, Allocator>& operator=(
const basic_string<CharType, Traits, Allocator>&& _Right
);
参数
_Ch
将分配的字符值。_Ptr
对于将要分配的C字符串的字符的指针到目标字符串。_Right
字符将分配给目标字符串的源字符串。
返回值
按成员函数分配新字符的字符串对象的引用。
备注
字符串能将新的字符值。新的值可以是字符串和C字符串或单个字符。可以使用 operator=,如果新值可以由单个参数描述,否则成员函数 分配,具有多个参数,可用于指定字符串的哪个部分将分配给目标字符串。
示例
// basic_string_op_assign.cpp
// compile with: /EHsc
#include <string>
#include <iostream>
int main( )
{
using namespace std;
// The first member function assigning a
// character of a certain value to a string
string str1a ( "Hello " );
str1a = '0';
cout << "The string str1 assigned with the zero character is: "
<< str1a << endl << endl;
// The second member function assigning the
// characters of a C-string to a string
string str1b;
const char *cstr1b = "Out There";
cout << "The C-string cstr1b is: " << cstr1b << "." << endl;
str1b = cstr1b;
cout << "Assigning the C-string cstr1a to string str1 gives: "
<< str1b << "." << endl << endl;
// The third member function assigning the characters
// from one string to another string in two equivalent
// ways, comparing the assign and operator =
string str1c ( "Hello" ), str2c ( "Wide" ), str3c ( "World" );
cout << "The original string str1 is: " << str1c << "." << endl;
cout << "The string str2c is: " << str2c << "." << endl;
str1c.assign ( str2c );
cout << "The string str1 newly assigned with string str2c is: "
<< str1c << "." << endl;
cout << "The string str3c is: " << str3c << "." << endl;
str1c = str3c;
cout << "The string str1 reassigned with string str3c is: "
<< str1c << "." << endl << endl;
}
要求
标头: <string>
命名空间: std