内容:string赋值操作。
#include <iostream>
#include <string>
using namespace std;
/*
函数原型:
1.string& operator=(const char* s); --> 将char*类型的字符串赋值给当前字符串
2.string& operator=(const string &s); --> 将字符串s赋值给当前字符串
3.string& operator=(char c); --> 将字符c赋值给当前字符串
4.string& assign(const char* s); --> 将char*类型的字符串赋值给当前字符串
5.string& assign(const string &s); --> 将字符串s赋值给当前字符串
6.string& assign(int n, char c); --> 将n个字符c赋值给当前字符串
7.string& assign(const char* s, int n); --> 把字符串的前n个字符赋值给当前字符串
*/
void test01()
{
string str1;
str1 = "hello world";
cout << str1 << endl;
string str2;
str2 = str1;
cout << str2 << endl;
string str3;
str3 = 'c';
cout << str3 << endl;
string str4;
str4.assign("hello world");
cout << str4 << endl;
string str5;
str5.assign(str4);
cout << str5 << endl;
string str6;
str6.assign(10, 'a');
cout << str6 << endl;
string str7;
// 把字符串的前n个字符赋值给当前字符串(注意空格也算一个字符串)
str7.assign("hello world", 7);
cout << str7 << endl;
};
int main()
{
test01();
return 0;
}