【转载,排版美化】c++中string的七个构造函数

连接:http://blog.csdn.net/qcontriver/article/details/36179083

//一、直接构造
string one("Lottery Winner");
cout << one << endl;

//二、20个元素的string对象,每个元素初始为$
string two(20,'$');
cout << two << endl;

//三、复制构造函数
string three(one);
cout << three << endl;

//重载 +=
one += " Oops!";
cout << one << endl;

//重载 + =
two = "Sorry! That was ";
three[0] = 'p';
string four;
four = two + three;
cout << four << endl;

//四、将five初始化为alls的前20个字符,即使超过了alls结尾也没事
char alls[] = "All`s well that ends well";
string five(alls, 20);
cout << five << endl;

//五、将six初始化alls的[6,10)之间的字符,6包含,10不包含。注:这是下标
string six(alls + 6,alls + 10);
cout << six << ", ";

//六、同six
string seven(&five[6], &five[10]);
cout << seven << "...\n";

//七、将eight初始化为four中,从位置7(下标)开始的16个字符,或者结尾;
string eight(four, 7, 16);
cout << eight << " in motion!" << endl;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值