String类构造函数全攻略

C++中新增添的string类在字符串使用的时候,使用方便,功能也更强大!

string的多种定义方式。看下面的一段代码:

// the construction of string
#include<iostream>
#include<string>
using namespace std;
int main()
{
	char name[]="Bill Gates";
	string s1,s2("Steven Jobs");
	cout<<"s2="<<s2<<endl;
	string s3(s2);
	cout<<"s3="<<s3<<endl;
	string s4(name,4);
	cout<<"s4="<<s4<<endl;
	string s5(s2,7,4);
	cout<<"s5="<<s5<<endl;
	string s6(5,'c');        
	cout<<"s6="<<s6<<endl;
	return 0;
}


程序很简单,这里总结一下几种定义方法。

1  string(const string&str) 用str复制构造新字符串

2  string  ( const  char *s,  unsigned  n)  用字符串常量或字符数组的前n个字符来构造串

3  string  (unsigned n, char c)  将字符c复制n次的字符串

4  string  (const string&str, unsigned pos, unsigned n)  将str以pos位置开始的字符开始的n个字符构造的串。

5  string  (const char*s)   用字符串常量或字符数组来构造新字符串

 


 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值