string容器3

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 }

 

原文链接: https://www.cnblogs.com/daitu66/p/17027076.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍

    string容器3

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/309820

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年2月16日 上午11:14
下一篇 2023年2月16日 上午11:15

相关推荐