string类的接口说明

1.string类对象的常见构造

看下面的代码:

#include<iostream>
#include<string>
using namespace std;

int main()
{
	string s1;              //构造空的string类对象,为空
	string s2("hello bit"); //构造类对象
	string s3(10, 'a');     //输出十个a
	string s4(s3);          //拷贝构造
	string s5(s3, 5);       //输出前五个,用前五个构造string对象
	cout << s2 << endl;
	cout << s3 << endl;
	cout << s4 << endl;
	cout << s5 << endl;

	system("pause");
	return 0;
}

2. string类对象的容量操作

size/length/clear/resize/reverse 接口测试
#include<iostream>
#include<string>
using namespace std;

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

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


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


	//将s中有效字符的个数增加到15个,多出的缺省位置用'\0'进行填充
	s.resize(15);                //aaaaaaaaaaaaaaa
	cout << s.size() << endl;    //15  
	cout << s.capacity() << endl;//15


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

}

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

	// 测试reserve参数小于string的底层空间大小时,是否会将空间缩小 
	s.reserve(50);
	cout << s.size() << endl;    //0
	cout << s.capacity() << endl;
	//111 当reverse的参数小鱼string的底层空间时(上边开好的100),reverse不会改变容量大小,见下边的注意点
}

int main()
{
	//Teststring1();
	Teststring2();
	system("pause");
	return 0;
}

== 注意事项==
1.想我家臭居居了
2. size()与length()方法底层实现原理完全相同,引入size()的原因是为了与其他容器的接口保持一 致,一般情况下基本都是用size()。
3. clear()只是将string中有效字符清空,不改变底层空间大小。
4. resize(size_t n) 与 resize(size_t n, char c)都是将字符串中有效字符个数改变到n个,不同的是当字 符个数增多时:resize(n)用0来填充多出的元素空间,resize(size_t n, char c)用字符c来填充多出的 元素空间。注意:resize在改变元素个数时,如果是将元素个数增多,可能会改变底层容量的大 小,如果是将元素个数减少,底层空间总大小不变。
5. reserve(size_t res_arg=0):为string预留空间,不改变有效元素个数,当reserve的参数小于 string的底层空间总大小时,reserver不会改变容量大小。

3.string类对象的访问操作

void teststring3()
{
	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;

	//s2[0] = 'H'; //代码编译失败,因为const类型对象不能修改
	//cout << s2 << endl;
	for (size_t i = 0; i < s1.size(); i++)
	{
		cout << s1[i] << endl;
	}
}
int main()
{
	//Teststring1();
	teststring3();
	system("pause");
	return 0;
}

4.string类对象的修改操作

void teststring4()
{
	string str;
	str.push_back(' ');      //在str后插入空格
	cout << str << endl;

	str.append("hello");     //在str后追加一个字符“hello”
	cout << str << endl;
	str += " world";         //在hello后边追加一个world
	cout << str << endl;

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


	//获取file的后缀
	string file("string.cpp");
	size_t pos = file.rfind('.');
	string suff(file.substr(pos, file.size() - pos));
	cout << suff << endl;

	//取出url的域名
	string url("http://www.cplusplus.com/reference/string/string/find/");
	cout << url << endl;
	size_t start = url.find("://");
	if (start == string::npos)
	{
		cout << "没找到" << endl;
	}
	start += 3;
	size_t finish = url.find('/', start);
	string address = url.substr(start, finish - start);
	cout << address << endl;

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

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值