《C++ string 完全指南:从基础到高效使用》

发布于:2025-07-29 ⋅ 阅读:(15) ⋅ 点赞:(0)

《C++ string 完全指南:从基础到高效使用》



一、STL简介

STL是 C++标准库的重要组成部分 ,不仅是一个可复用的组件库,而且是
一个包罗 数据结构与算法的软件框架


在这里插入图片描述


二、string类

1.C语言中的字符串

C语言中,字符串是以’\0’结尾的一些字符的集合,为了操作方便,C标准库中提供了一些str系列
的库函数,但是这些库函数与字符串是分离开的,不太符合OOP的思想,而且底层空间需要用户
自己管理,稍不留神可能还会越界访问。


2.标准库中的string类

在使用string类时,必须包含 #include头文件以及using namespace std;


2.1 auto关键字

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

在这里插入图片描述


2.2 范围for

在这里插入图片描述
在这里插入图片描述


2.3 string类的常用接口说明

2.3.1 string迭代器(正向和反向)

在这里插入图片描述在这里插入图片描述


2.3.2 string类对象常见构造

在这里插入图片描述
在这里插入图片描述


2.3.3 string类对象的容量操作

在这里插入图片描述
在这里插入图片描述

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述在这里插入图片描述


2.3.3 string类对象的访问及遍历操作

在这里插入图片描述


2.3.4 string类对象的修改操作

在这里插入图片描述
在这里插入图片描述在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

在这里插入图片描述


2.3.5 string类非成员函数

在这里插入图片描述

在这里插入图片描述


源代码展示

代码如下(示例):

#include<iostream>
#include<string>
#include<assert.h>
using namespace std;
void Test1()
{
	string s1 = "hello wkm!";
	//普通迭代器
	string::iterator it = s1.begin();//指向第一个位置
	while (it != s1.end())
	{
		cout << *it << " ";
		++it;
	}
	cout << endl;
	const string s2 = "hello wkm!";//const对象
	//const 反向迭代器 不能改变字符串中的值
	string::const_iterator itt = s2.begin();//指向最后一个字符的下一个位置
	while (itt != s2.end())
	{
		cout << *itt << " ";
		++itt;
	}
}
void Test2()
{
	string s1 = "hello wkm!";
	//普通反向迭代器
	string::reverse_iterator rit = s1.rbegin();//指向最后一个字符位置
	while (rit != s1.rend())
	{
		cout << *rit << " ";
		++rit;
	}
	cout << endl;
	const string s2 = "hello wkm!";//const对象
	//const 反向迭代器 不能改变字符串中的值
	string::const_reverse_iterator ritt = s1.rbegin();//指向最后一个字符的位置
	while (ritt != s1.rend())
	{
		cout << *ritt << " ";
		++ritt;
	}
}
void Test3()
{
	//1. 使用我们的默认构造函数,不需要传参。
	string s1;
	s1 = "hello wkm!";
	//2. 使用的是拷贝构造来初始化。
	string s2(s1);
	//3. 使用一个string的某段区间初始化,其中pos是字符串下标,npos是指无符号整数的最大值。
	string s3(s2, 1, 7);
	//4. 使用的是某个字符数组初始化。
	string s4("hello world!");
	//5. 使用的是某个字符数组前n个字符来初始化
	string s5("hello world!", 5);
	//6. 使用的是n个c字符初始化。
	string s6(7, 'a');
	//7. 使用的是某段迭代器区间初始化。
	string s7(s1.begin(), s1.end());
	//赋值运算符重载初始化
	string s8 = "hello wkm!";
	cout << s1 << endl;
	cout << s2 << endl;
	cout << s3 << endl;
	cout << s4 << endl;
	cout << s5 << endl;
	cout << s6 << endl;
	cout << s7 << endl;
	cout << s8 << endl;
}
void Test4()
{
	string s("hello wkm!");
	cout << s.size() << endl;//有效长度
	cout << s.length() << endl;//有效长度
	cout << s.capacity() << endl;//容量大小
	cout << s.max_size() << endl;//最大大小
}
void TestCapacity()
{
	string s;
	size_t sz = s.capacity();
	cout << "making s grow:\n";
	for (int i = 0; i < 100; ++i)
	{
		s.push_back('c');
		if (sz != s.capacity())
		{
			sz = s.capacity();
			cout << "capacity changed: " << sz << '\n';
		}
	}
}
void TestEmpty()
{
	string s1("");//空串
	string s2("hello ");
	if (s1.empty())
	{
		cout << "s1为空串" << endl;
	}
	else
	{
		cout << "s1不为空串" << endl;
	}
	if (s2.empty())
	{
		cout << "s2为空串" << endl;
	}
	else
	{
		cout << "s2不为空串" << endl;
	}
}
void TestClear()
{
	string s1("hello world!");
	cout << "s1的有效长度为:" << s1.size() << endl;
	cout << "s1的容量大小为:" << s1.capacity() << endl;
	s1.clear();
	cout << "s1的有效长度为:" << s1.size() << endl;
	cout << "s1的容量大小为:" << s1.capacity() << endl;
	if (s1.empty())
	{
		cout << "s1是空串" << endl;
	}
}
void Test5()
{
	string s1("hello world!");
	cout << "reserve测试:" << endl;
	cout << s1 << endl;
	cout << "s1的有效长度为:" << s1.size() << endl;
	cout << "s1的容量大小为:" << s1.capacity() << endl;
	s1.reserve(5);
	cout << s1 << endl;
	cout << "s1的有效长度为:" << s1.size() << endl;
	cout << "s1的容量大小为:" << s1.capacity() << endl;
	s1.reserve(13);
	cout << s1 << endl;
	cout << "s1的有效长度为:" << s1.size() << endl;
	cout << "s1的容量大小为:" << s1.capacity() << endl;
	s1.reserve(25);
	cout << s1 << endl;
	cout << "s1的有效长度为:" << s1.size() << endl;
	cout << "s1的容量大小为:" << s1.capacity() << endl;
	cout << endl;
	cout << "resize测试:" << endl;
	string s2("hello world!");
	cout << s2 << endl;
	cout << "s2的有效长度为:" << s2.size() << endl;
	cout << "s2的容量大小为:" << s2.capacity() << endl;
	s2.resize(5);
	cout << s2 << endl;
	cout << "s2的有效长度为:" << s2.size() << endl;
	cout << "s2的容量大小为:" << s2.capacity() << endl;
	s2.resize(10, 'x');
	cout << s2 << endl;
	cout << "s2的有效长度为:" << s2.size() << endl;
	cout << "s2的容量大小为:" << s2.capacity() << endl;
	s2.resize(25, 'x');
	cout << s2 << endl;
	cout << "s2的有效长度为:" << s2.size() << endl;
	cout << "s2的容量大小为:" << s2.capacity() << endl;
}
void Test6()
{
	string s1("hello world!");
	cout << s1 << endl;
	cout << "s1的有效长度为:" << s1.size() << endl;
	cout << "s1的容量大小为:" << s1.capacity() << endl;
	s1.reserve(100);//先扩容
	cout << "扩容后s1的有效长度为:" << s1.size() << endl;
	cout << "扩容后s1的容量大小为:" << s1.capacity() << endl;
	s1.shrink_to_fit();//再缩容
	cout << "缩容后s1的有效长度为:" << s1.size() << endl;
	cout << "缩容后s1的容量大小为:" << s1.capacity() << endl;
}
void Test7()
{
	string s1("hello wkm!");
	for (int i = 0; i < s1.size(); i++)
	{
		cout << s1[i] << " ";
	}
	cout << endl;
	for (int i = 0; i < s1.size(); i++)
	{
		cout << s1.at(i) << " ";
	}
}
void Test8()
{
	string s1("hello wkm!");
	cout << s1.front() << endl;
	cout << s1.back() << endl;
}
void Test9()
{
	string s("hello wkm!");
	//追加一个!
	s.push_back('!');
	cout << s << endl;
	s += '!';
	cout << s << endl;
	s.insert(0, 1, '!');
	cout << s << endl;
	//追加一个字符串
	s.append("hello ");
	cout << s << endl;
	s += "world!";
	cout << s << endl;
	//在下标0处追加字符串
	s.insert(0, "hello ");
	cout << s << endl;
}
void Test10()
{
	string s1("hello world!");
	string s2;
	//直接用s1替换s2
	s2.assign(s1);
	cout << s2 << endl;
	//用s1的某段区间替换s2
	s2.assign(s1, 6);
	cout << s2 << endl;
	string s3("i am wkm!");
	//用s1替换掉2下标长度为2的区间
	s3.replace(2, 2, s1);
	cout << s3 << endl;
	//用字符数组前n个字符替换
	s3.replace(0, 2, "hhhh", 2);
	cout << s3 << endl;
}
void Test11()
{
	string s("hello wkm!");
	//删除最后一个字符
	s.pop_back();
	cout << s << endl;
	//删除迭代器所指字符
	s.erase(s.begin());
	cout << s << endl;
	//删除0下标长度为3的一段区间
	s.erase(0, 3);
	cout << s << endl;
	//删除一段迭代器区间
	s.erase(s.begin(), s.end() - 2);
	cout << s << endl;
}
void TestSwap()
{
	string s("hello wkm!");
	string p("hello world!");
	cout << s << endl;
	cout << p << endl;
	s.swap(p);
	cout << s << endl;
	cout << p << endl;
}
void Test12()
{
	string s("hello wkm!");
	//在字符串s中寻找w
	size_t pos = s.find('w');
	//从下标6开始截取长度为5的字符串
	string str = s.substr(pos, 5);
	cout << str << endl;
}
void Test13()
{
	string str("Please, replace the vowels in this sentence by asterisks.");
	//从str字符串中任意匹配aeiou
	size_t found = str.find_first_of("aeiou");
	//找不到返回npos
	while (found != string::npos)
	{
		str[found] = '*';
		//从下一个位置找
		found = str.find_first_of("aeiou", found + 1);
	}
	cout << str << endl;
}
int main()
{
	//Test1();
	//Test2();
	//Test3();
	//Test4();
	//TestCapacity();
	//TestEmpty();
	//TestClear();
	//Test5();
	//Test6();
	//Test7();
	//Test8();
	//Test9();
	//Test10();
	//Test11();
	//TestSwap();
	//Test12();
	Test13();
}

总结

本文介绍了string的使用,感谢你的关注


网站公告

今日签到

点亮在社区的每一天
去签到