Primer plus C++ 第十六章 string类_构造函数



/*
 * 介绍string类;
 * 我们先来了解一下string的构造函数:
 *  1)将string对象初始化为s指向的NBTS
 *	string(const char *s)
 *	2) 创建一个包含n个元素的string对象,其中每个元素都被初始化为字符c
 *	string(size_type n, char c)
 *	3) 将string对象初始化为对象str中从位子pos开始到结尾的字符,或从位置pos开始的n个字符
 *	string(const string & str, string size_type n=npos)
 *	4)创建一个默认的string对象,长度为0
 *	string()
 *	5)将string对象初始化为s指向的NBTS中的前n个字符
 *	string(const char * s, size_type n)
 *	6)将string对象初始化为区间 [begin, end]内的字符,其中begin和end的行为就像指针,用于制定位子
 *	template<class Iter>
 *	string(Iter begin, Iter end)
 *
 */

#include<iostream>
#include<string>
int main() {
	using namespace std;
	string one("Lottery Winner!");  //构造函数1
	cout << one << endl;
	string two(20, 's');   // 构造函数2:将string对象two初始化为由20个$字符组成的字符串
	cout << two << endl;
	string three(one);  // 构造函数3:复制构造函数将string对象three初始化为string对象one
	cout << three << endl;
	one += "Oops!";    // 重载函数:重载的+=操作符将字符串“Oops!”附加到字符串one后面
	two = "Sorry! That was";
	three[0] = 'p';
	string four;   //构造函数4
	four = two + three;  // 重载函数
	cout << four << endl;
	char alls[] = "All's well that ends well";
	string five(alls, 20);  // 构造函数5
	cout << five << "!\n";
	string six(alls + 6, alls + 10); // 构造函数6
	cout << six << ",";
	string seven(&five[6], &five[10]);
	cout << seven << "...\n";
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值