c++string类

1.string是表示字符串的字符串类
2.该类的接口与常规容器的接口基本相同。

string类对象的常见构造

void TestString()
{
	string s1;//构造一个string空类对象s1
	string s2("hello bit");//用c字符串的形式构造一个string类s2
	string s3(10, 'a');//用10个字符‘a’构造string类对象s3
	string s4(s2);//拷贝构造s4
	string s5(s3, 5);//用s3中前5个字符构造string对象s5
}

string类对象的容量操作

void TestString1()
{
	string s("hello world");
	//string s;
	cout << s.length() << endl;
	cout << s.size() << endl;
	cout << s.capacity() << endl;
	cout << s << endl;

	//将s中的字符串清空,注意情空时只是将size清0,不改变底层空间的大小
	s.clear();
	cout << s.size() << endl;
	cout << s.capacity() << endl;
	cout << s << endl;


	//将s中有效字符个数增加到10个,多出位置用‘a’进行填充
	//‘aaaaaaaaaa’
	s.resize(10,'a');
	cout << s.size() << endl;
	cout << s.capacity() << endl;
	cout << s << endl;

	//将s中有效字符个数增加到15个,多出位置用缺省值‘\0’进行填充
	//'aaaaaaaaaa\0\0\0\0\0'
	//注意此时s中有效字符个数已经增加到15个
	//s.resize(15,'1');
	s.resize(15);
	cout << s.size() << endl;
	cout << s.capacity() << endl;
	cout << s << endl;

	//将s中的有效字符个数缩小到5个
	s.resize(5);
	cout << s.size() << endl;
	cout << s.capacity() << endl;
	cout << s << endl;


	//增大
	s.resize(16);
	cout << s.size() << endl;
	cout << s.capacity() << endl;
	cout << s << endl;
}


void TestString2()
{
	string s;
	//测试reserve是否会改变string中有效元素的个数
	s.reserve(100);
	cout << s.size() << endl;
	cout << s.capacity() << endl;


	//测试resever参数小于string的底层大小时,是否会将空间缩小
	s.reserve(50);
	cout << s.size() << endl;
	cout << s.capacity() << endl;
}

1.size()与length()方法底层实现原理完全相同,引入size的原因是为了与其他容器接口保持一致。
2.clear只是将string中有效字符清空,不改变底层空间大小。
3.resize(size_t n,char c)将字符串中的有效字符个数改变为nge,用字符c来填充多出来元素,当元素个数增多,可能会改变底层容量的大小。
4.reserve,为string预留空间,不改变有效元素的个数,当reserve的参数小于string的底层空间总大小时,reserver不会改变容量大小。reserve就是一个增容作用,自动增容,第一次扩大2倍,后续按1.5倍增容。提高了效率,减少了增容的操作。

string类对象的访问操作

void TestString()
{
	string s1("hello world");
	const string s2("hello world");
	cout<<s1<< " "<< s2 << endl;
	cout << s1[0] << " " << s2[0] << endl;
	
	s1[0] = 'H';
	cout << s1 << endl;
	for (size_t i = 0; i < s1.size(); i++)
	{
		cout << s1[i] << endl;
	}

	//s2[0]='H' ; 编译失败,因为const类型对象不能修改
}

string类对象的修改操作

void TestString()
{
	string s1("hello world");
	const string s2("hello world");
	cout<<s1<< " "<< s2 << endl;
	cout << s1[0] << " " << s2[0] << endl;
	
	s1[0] = 'H';
	cout << s1 << endl;
	for (size_t i = 0; i < s1.size(); i++)
	{
		cout << s1[i] << endl;
	}

	//s2[0]='H' ; 编译失败,因为const类型对象不能修改
}

void TestString2()
{
	string str;
	str.push_back(' ');//在str后面插入空格
	str.append("hello");//在str后面追加一个字符串hello
	str += 'b';//在str后追加一个字符‘b’
	str += "it";//在str后追加一个字符串“it”

	cout << str << endl;
	cout << str.c_str() << endl;//以c语言的方式来打印字符串

	//获取file的后缀
	string file("string.cpp");
	size_t pos = file.rfind('.');//从后开始找字符为.
	string suffix(file.substr(pos, file.size() - pos));//截取字符串
	cout << suffix << endl;

	//nops是string里面的一个静态成员变量
	//static const size_t npos=-1;
	//取出url中的域名
	//url统一资源定位符
	string url("http://www.cplusplus.com/reference/string/string/find/");
	cout << url << endl;
	size_t start = url.find("://");//找到://的位置
	if (start == string::npos)
	{
		cout << "invalid url" << endl;
		return;
	}
	start += 3;
	size_t finish = url.find('/', start);//从start位置开始找到第一个/的位置
	string address = url.substr(start, finish - start);
	cout << address << endl;

	//删除url的协议前缀
	pos = url.find("://");
	url.erase(0, pos + 3);
	cout << url << endl;



}


//利用reserve提高插入数据的效率,避免增容带来的开销
//
void TestPushBack()
{
	string s;
	size_t sz = s.capacity();
	cout << "making s grow:\n";
	for (int i = 0; i < 100; ++i)
	{
		s += 'c';
		cout << s << endl;
		if (sz != s.capacity())
		{
			sz = s.capacity();
			cout << "capacity changed:" << sz << '\n';
		}
	}
}

1.在string尾部追加字符时,s.push_back / s.append() /s+=‘'三种的实现方式差不多,一般情况下string类的+=操作比较多,+=操作不仅可以连接单个字符,还还可以连接字符串。
2.对string操作,如果能够大概预估到放多少字符,可以先通过reserve把空间预留好。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值