C++ 容器 string类

C++ 容器 string类

一、string类的构造函数
1 默认构造 string s1;
2 拷贝构造 string s2(s1);
3 有参构造 string s3(“aaa”);
4 两个参数有参构造 string s4(3,‘c’);

// string类的构造函数
    string s1;       //默认构造
	string s2(s1);   //拷贝构造
	string s3("aaa");    //有参构造
	string s4(3, 'c');    //两个参数有参构造
	cout << "字符串s1为:"<<s1 << endl;
	cout <<"字符串s2为:"<< s2 << endl;
	cout <<"字符串s3为:"<< s3 << endl;
	cout <<"字符串s4为:"<< s4 << endl;
字符串s1为:
字符串s2为:
字符串s3为:aaa
字符串s4为:ccc

二、string类的赋值操作
1、string& assign(const char *s,int n); 把字符串s的前n个字符串赋给当前的字符串
2、string& assign(const string &s,int start,int n); 将s从start开始n个字符赋值给字符串

//string类的赋值操作
string s5;
	//string& assign(const char *s,int n);  把字符串s的前n个字符赋给当前的字符串
	s5.assign("asddfsdsgdsgd", 8);
	cout << "s5=" << s5 << endl;

	//string& assign(const string &s,int start,int n); 将s从start开始n个字符赋值给字符串(从0开始计算)
	string s6 =</
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值