string的赋值操作
功能描述:
给string字符串进行赋值
赋值的函数原型:
1 string& operator=(const char *s);//char*类型字符串 赋值给当前的字符串 2 string& operator=(const string &s)//把字符串s赋给当前的字符串 3 string& operator=(char c);//字符赋值给当前字符串 4 string& assign(const char *s)//把字符串s赋给当前的字符串 5 string& assign(const char *s,int n);//把字符串s的前n个字符赋给当前的字符串 6 string& assign(const string &s);//把字符串s赋给当前字符串 7 string& assign(int n,char c);//用n个字符c赋给当前字符串
示例:
1 #include<iostream> 2 #include<string> 3 using namespace std; 4 //string& operator=(const char *s);//char*类型字符串 赋值给当前的字符串 5 //string& operator=(const string &s)//把字符串s赋给当前的字符串 6 //string& operator=(char c);//字符赋值给当前字符串 7 //string& assign(const char *s)//把字符串s赋给当前的字符串 8 //string& assign(const char *s,int n);//把字符串s的前n个字符赋给当前的字符串 9 //string& assign(const string &s);//把字符串s赋给当前字符串 10 //string& assign(int n,char c);//用n个字符c赋给当前字符串 11 void test01() 12 { 13 string str1; 14 str1="hello word"; 15 cout<<"str1 = "<<str1<<endl; 16 string str2; 17 str2=str1; 18 cout<<"str2 = "<<str2<<endl; 19 string str3; 20 str3='a'; 21 cout<<"str3 = "<<str3<<endl; 22 string str4; 23 str4.assign("hello word"); 24 cout<<str4<<endl; 25 string str5; 26 str5.assign("hello C++",5); 27 cout<<"str5 = "<<str5<<endl; 28 string str6; 29 str6.assign(str5); 30 cout<<str6<<endl; 31 string str7; 32 str7.assign(5,'a'); 33 cout<<str7<<endl; 34 35 } 36 int main() 37 { 38 test01(); 39 system("pause"); 40 }
标签:容器,const,string,char,字符串,赋给,assign From: https://www.cnblogs.com/daitu66/p/17027076.html