C++STL:string类的常用接口及说明

我们都知道,在C语言中,字符串是以’\0’结尾的一些字符的集合,为了操作方便,C标准库还提供一些库函数,供我们使用,但是这些库函数与字符串是分离的,而且底层空间也需要用户自己管理,较为麻烦。
所以C++在标准库中提供了string类,大大方便了我们对字符串的操作。接下来我们就来看看string类的常见接口及使用方法。

1.string类对象的常见构造
函数名功能
string()构造空的string类对象,即空字符串
string(const char* s)用C-string来构造string类对象
string(size_t n, char c)string类对象中包含n个字符c
string(const string&s)拷贝构造函数
string(const string&s, size_t n)将s中的前n个字符除去,用剩下的字符构造新的string类对象
void TestString()
{
	string s1;					// ""
	string s2("C string");		// "C string"
	string s3(10, 'c');			// "cccccccccc"
	string s4(s3);				// "cccccccccc"
	string s5(s2, 5);			// "ing"
	string s6(s2, 2);			// "string"
}
2.string类对象的容量操作
函数名功能
size_t size() const返回字符串有效字符长度
size_t length() const返回字符串有效字符长度
size_t capacity ( ) const返回空间总大小
bool empty ( ) const检测字符串释放为空串,是返回true,否则返回false
void clear()清空有效字符
void resize ( size_t n, char c )将有效字符的个数改成n个,多出的空间用字符c填充
void resize ( size_t n )将有效字符的个数改成n个,多出的空间用0填充
void reserve ( size_t res_arg=0 )为字符串预留空间
void TestString1()
{
	// 注:string类对象支持直接用cin/cout进行输入输出
	string s("hello world!");
	cout << s.length();					// 12
	cout << s.size() << endl;			// 12
	cout << s.capacity() << endl;		// 15
	cout << s << endl;					// "hello world!"

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

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

	//将s中有效字符个数增加到50个,多出位置用缺省值'\0'填充
	s.resize(50);
	cout << s.size() << endl;			// 50
	cout << s.capacity() << endl;		// 63
	cout << s << endl;					// "aaaaaaaaaa"

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

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

	s.reserve(200);
	cout << s.size() << endl;			// 0
	cout << s.capacity() << endl;		// 207

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

通过测试可以看出:

  • 1.size()和length()底层实现原理完全相同,引入size()的原因是为了与其他容器的接口保持一致,
  • 2.clear()只是将string中有效字符清空,不改变底层空间大小,
  • 3.resize 在改变元素个数时,如果是将元素个数增多,可能会改变底层容量的大小,如果是将元素个数减少,底层空间总大小不变,
  • 4.reserve:为string预留空间,不改变有效元素个数,当其参数小于string的底层空间总大小时,reserve不会改变容量大小。
3.string类对象的访问操作
函数名功能
char& operator[](size_t pos)返回pos位置的字符,非const string类对象调用
const char& operator[](size_t pos) const返回pos位置的字符,const string类对象调用
void TestString()
{
	string s1("hello world!");
	const string s2("hello world!");
	cout << s1 << " " << s2 << endl;			// "hello world!" "hello world!"
	cout << s1[0] << " " << s2[0] << endl;		// 'h' 'h'

	s1[0] = 'H';
	cout << s1 << endl;			// 'H'
	//s2[0] = 'H';	// 代码编译失败,const类型对象不能修改
	//cout << s2 << endl;

	for (size_t i = 0; i < s1.size(); ++i)
		cout << s1[i] << endl;
}
4.string类对象的修改操作
函数名功能
void push_back(char c)尾插字符c
string& append(const char* s)追加一个字符串
string& operator+=(const string& str)追加字符串str
string& operator+=(const char* s)追加C风格字符串
string& operator+=(char c)追加字符c
const char* c_str() const返回C风格字符串
size_t find(char c, size_t pos = 0) const从pos位置开始往后找字符c,返回该字符在字符串中的位置
size_t rfind(char c, size_t pos = npos)从pos位置开始往前找字符c,返回该字符在字符串中的位置
string substr(size_t pos = 0, size_t n = npos) const从pos位置开始,截取n个字符,然后将其返回
void TestString()
{
	string str;
	str.push_back('h');			// 在str后插入一个字符'h'
	str.append("ello");			// 在str后追加一个字符串"ello"
	str += ' ';					// 在str后追加一个空格
	str += "world!";			// 在str后追加一个字符串"world!"
	cout << str << endl;		// "hello world!"
	cout << str.c_str() << endl;	// "hello world!"

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

	// npos是string里面的一个静态成员变量
	// static const size_t npos = -1
	// 获取ur1中的域名
	string ur1("http://www.cplusplus.com/reference/string/string/find/");
	cout << ur1 << endl;			// "http://www.cplusplus.com/reference/string/string/find/"
	size_t start = ur1.find("://");
	if (start == string::npos)
	{
		cout << "invalid ur1" << endl;
		return;
	}
	start += 3;
	size_t finish = ur1.find('/', start);
	string address = ur1.substr(start, finish - start);
	cout << address << endl;		// "www.cplusplus.com"

	// 删除ur1的协议前缀
	pos = ur1.find("://");
	ur1.erase(0, pos + 3);
	cout << ur1 << endl;			// "www.cplusplus.com/reference/string/string/find/"
}

void TestPushBack()
{
	string s;
	size_t sz = s.capacity();
	cout << "making s grow:\n";
	for (int i = 0; i < 100; ++i)
	{
		s += 'c';
		if (sz != s.capacity())
		{
			sz = s.capacity();
			cout << "capacity changed: " << sz << '\n';		// 31、47、70、105
		}
	}
}

// 使用reverse可以避免增容带来的开销,提高插入数据的效率
void TestPushBack_P()
{
	string s;
	s.reserve(100);
	size_t sz = s.capacity();

	cout << "making s grow:\n";
	for (int i = 0; i < 100; ++i)
	{
		s += 'c';
		if (sz != s.capacity())
		{
			sz = s.capacity();
			cout << "capacity changed: " << sz << '\n';
		}
	}
}

通过测试,我们可以总结出:

  • 1.在string尾部追加字符时,s.push_back( c ) /s.append(1, c)/s += 'c’三种方式相差不大,一般情况下+=操作用的较多,因为+=操作不仅可以连接单个字符,还可以连接字符串,
  • 2.对string操作时,如果能够大概预估到放多少字符,可以先通过reverse把空间预留好。这样可以避免增容带来的开销,提高插入数据的效率。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值