代码
#include<iostream>#include<string>using namespace std;//string的赋值操作/*① string& operator=(const char* s); //char*类型的字符串赋值给当前的字符串② string& operator=(const string &s); //把字符串s赋值给当前的字符串③ string& operator=(char c); //符赋值给当前的字符串④ string& assign(const char *s); //字符串s赋值给当前的字符串⑤ string& assign(const char *s,int n); //把字符串s的前n个字符赋值给当前的字符串⑥ string& assign(const string &s); //把字符串s赋值给当前的字符串⑦ string& assign(int n,char c); //使用n个字符赋值给当前的字符串*/void test01(){ string s1;//① s1 = "hello world"; cout << "s1 = " << s1 << endl; string s2;//② s2 = s1; cout << "s2 = " << s2 << endl; string s3;//③ s3 = 'w'; cout << "s3 = " << s3 << endl; string s4;//④ s4.assign("hello world"); cout << "s4 = " << s4 << endl; string s5;//⑤ s5.assign("hello world",5); cout << "s5 = " << s5 << endl; string s6;//⑥ s6.assign(s5); cout << "s6 = " << s6 << endl; string s7;//⑦ s7.assign(10,'w'); cout << "s7 = " << s7 << endl;}int main() { test01(); return 0;}
运行结果
