目录
一、介绍
二、string类对象的构造
string类有如下构造方法:
类对象的容量操作
类对象访问及遍历
string对象的修改操作:
std::string::insert
std::string::erase
std::string::c_str
std::string::find
std::string::substr
一、介绍
1. 字符串是表示字符序列的类
2. 标准的字符串类提供了对此类对象的支持,其接口类似于标准字符容器的接口,但添加了专门用于操作单字节字符字符串的设计特性。
3. string类是使用char(即作为它的字符类型,使用它的默认char_traits和分配器类型。
4. string类是basic_string模板类的一个实例,它使用char来实例化basic_string模板类,并用char_traits和allocator作为basic_string的默认参数。
5. 这个类独立于所使用的编码来处理字节:如果用来处理多字节或变长字符(如UTF-8)的序列,这个类的所有成员(如长度或大小)以及它的迭代器,将仍然按照字节(而不是实际编码的字符)来操作。
注意:使用string类时,必须包含#include头文件以及using namespace std;
二、string类对象的构造
string类有如下构造方法:

代码案例:
void test()
{
	string s1; // 构造空的string类对象s1
	string s2("hello string"); // 用C格式字符串构造string类对象s2
	string s3(s2); // 拷贝构造s3
	cout << s2 << " " << s3 << endl;
	char s4[] = "hello world!!!";
	string s5(s4);//拷贝构造s5,从char*s4拷贝过来
	cout << s5 << endl;
	string s6(s4, 3);//拷贝构造,从s4的前3个位置字符
	cout << s6 << endl;
	string s7(4, 'c');//构造s7,由4个字符‘c’构成的字符串
	cout << s7 << endl;
} 

类对象的容量操作
| 函数名称 | 功能说明 | 
| size | 返回字符串有效长度 | 
| length | 返回字符串有效长度 | 
| capacity | 返回空间总大小 | 
| empty |   检测字符串释放为空串,是 返回true,否 返回false  | 
| clear | 清空有效字符 | 
| reserve | 为字符串预留空间 | 
| resize | 将有效字符的个数改成n个,多出的空间用字符C填充 | 
注意:
1. size()与length()方法底层实现原理完全相同,引入size()的原因是为了与其他容器的接口保持一致,一般情况下基本都是用size()。
2. clear()只是将string中有效字符清空,不改变底层空间大小。
3. resize(size_t n) 与 resize(size_t n, char c)都是将字符串中有效字符个数改变到n个,不同的是当字符个数增多时:resize(n)用0来填充多出的元素空间,resize(size_t n, char c)用字符c来填充多出的元素空间。注意:resize在改变元素个数时,如果是将元素个数增多,可能会改变底层容量的大小,如果是将元素个数减少,底层空间总大小不变。
4. reserve(size_t res_arg=0):为string预留空间,不改变有效元素个数,当reserve的参数小于string的底层空间总大小时,reserver不会改变容量大小。
代码案例:
void Teststring1()
{
	// 注意:string类对象支持直接用cin和cout进行输入和输出
	string s("hello, string!!!");
	cout << s.size() << endl;
	cout << s.length() << endl;
	cout << s.capacity() << endl;
	cout << s << endl;
	// 将s中的字符串清空,注意清空时只是将size清0,不改变底层空间的大小
	cout << "clear:" << endl;
	s.clear();
	cout << s.size() << endl;
	cout << s.capacity() << endl;
	// 将s中有效字符个数增加到10个,多出位置用'a'进行填充
	// “aaaaaaaaaa”
	cout << "resize:" << endl;
	s.resize(10, 'a');
	cout << s.size() << endl;
	cout << s.capacity() << endl;
	// 将s中有效字符个数增加到15个,多出位置用缺省值'\0'进行填充
	// "aaaaaaaaaa\0\0\0\0\0"
	// 注意此时s中有效字符个数已经增加到15个
	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;
} 

对于不同的编译器,string初始开辟的空间有所不同,下面是VS2019编译器下的运行结果。
初始开辟的空间大小及每次扩容的大小
void TestPushBackReserve()
{
	string s;
	s.reserve(100);
	size_t sz = s.capacity();
	cout << "making s grow:\n";
	for (int i = 0; i < 1000; ++i)
	{
		s.push_back('c');
		if (sz != s.capacity())
		{
			sz = s.capacity();
			cout << "capacity changed: " << sz << '\n';
		}
	}
} 

类对象访问及遍历
| 函数名称 | 功能说明 | 
| operator[pos] | 返回pos位置的字符 | 
| begin+end | begin获取一个字符迭代器+end获取最后一个位置迭代器 | 
| rbegin+rend |   (逆向)rbegin获取最后一个字符迭代器+rend获取首个位置迭代器  | 
| 范围for | C++11支持更简洁的范围for的鑫遍历方式 | 
代码案例:
void test()
{
     string s("hello Bit");
	//for遍历operator[]
	for (int i = 0; i < s.size(); i++)
	{
		cout << s[i] << " ";
	}
	cout << "    operator[]:" << endl;
	//迭代器遍历 begin+end
	string::iterator it = s.begin();//创建迭代器
	while (it != s.end())
	{
		cout << *it <<" ";
		++it;
	}
	cout << "   迭代器begin+end" << endl;
	//迭代器 rbegin+rend
	string::reverse_iterator rit = s.rbegin();//创建迭代器
	while (rit != s.rend())
	{
		cout << *rit << " ";
		++rit;
	}
	cout<< "    迭代器rbegin+rend" << endl;
	//范围for
	for (auto ch : s)
	{
		cout << ch << " ";
	}
	cout << "    范围for" << endl;
} 
  
string对象的修改操作:
| 函数名称 | 功能说明 | 
| push_back | 在字符串后面尾插字符 | 
| append | 在字符串后面追加一个字符 | 
| operator+= | 在字符串后面追加字符、字符串 | 
代码案例:
void push()
{
    string s("hello");//构造并初始化对象
	cout << s << endl;
	s.push_back(' ');//尾插一个字符
	cout << s << endl;
	s.append("str");//追加字符串
	cout << s << endl;
	s += "ing";//追加字符串
	cout << s << endl;
} 

std::string::insert
| 函数名称 | 功能说明 | 
 string& insert (size_t pos, const string& str);  |   在原串下标为pos的字符前插入字符串str  | 
 string& insert (size_t pos, const string& str, size_t subpos, size_t sublen);  |   str从下标为subpos开始数的sublen个字符插在原串下标为pos的字符前  | 
 string& insert (size_t pos, const char* s);  | 在原串pos位置前插入字符串*s | 
 string& insert (size_t pos, const char* s, size_t n);  | 在原串pos位置前插入字符串s前n个字符 | 
 string& insert (size_t pos, size_t n, char c);  |   在原串下标为pos的字符前插入n个字符c  | 
注意:pos是指下标,从0开始 ,都是在之前插入
代码案例:
void test()
{
	string s1("hello string!!!");
	cout << s1 << endl;
	string s2(s1);
	string s3("it");
	s2.insert(6, s3);//在小标为6的位置前插入s3
	cout << s2 << endl;
	string s4(s1);
	s4.insert(6, s3, 1, 1);//在下标为6的位置前插入s3的下标为1数的一个字符
	cout << s4 <<endl;
	string s5("hello ");
	s5.insert(5, "world");//在下标为5的位置插入字符串“world”
	cout << s5 << endl;
	string s6("hello");
	s6.insert(4, "ABCD", 2);//在下标为4的位置插入字符串“ABC”的前2个字符
	cout << s6 << endl;
	string s7("hello");
	s7.insert(2, 3, 'A');//在下标为2的位置前插入3个字符'A'
	cout << s7 << endl;
} 

std::string::erase
| string& erase(size_t pos,size_t len) |   删除pos位置起len个长度字符 len过长就有多少删多少 pos不是指的下标  | 
代码案例:
void test()
{
	string s1{ "hello string" };
	s1.erase(6, 5);//删除第六个位置起的五个字符
	cout << s1 << endl;
} 

std::string::c_str
| Get C string equivalent (public member function) | 返回C字符串 | 
代码案例:
如下,以C形式读取文件,如果使用string去运行会报错,只能使用c_str转换为C使用才能编译成功:
void test()
{
	string filename("test.cpp");  //通过string表示出文件名文件名
	FILE* fout = fopen(filename.c_str(), "r");//读取该文件
	assert(fout);//断言
	char ch = fgetc(fout);//打印文件内容
	while (ch != EOF)
	{
		cout << ch;
		ch = fgetc(fout);//打印文件内容
	}
}
//这里的test.cpp文件是我的编译下的.cpp源文件 

打印结果案例:
void test()
{
	string filename("test.cpp");
	cout << filename << endl;
	cout << filename.c_str() << endl;
	filename += '\0';
	filename += "string.cpp";
	cout << filename << endl;//string对象以size为准
	cout << filename.c_str() << endl;//常量字符串对象以\0为准
} 

为什么打印结果不一样呢?
string对象的结束是以size大小(总长度)为准
常量字符串(c_str)结束是以‘\0’为准(遇到就截止)
std::string::find
| 函数名称 | 功能介绍 | 
 size_t find (const string& str, size_t pos = 0)  |   在原串中从pos位置开始,找str在原串中的下标位置,找到返回下标,没找到返回-1 pos是下标 pos可以不写,就从0下标位置开始往后找  | 
 size_t find (const char* s, size_t pos = 0)  | 同上 | 
 size_t find (const char* s, size_t pos, size_t n)  | 在原串中从pos位置,找s中的n个字符首次出现的下标位置 | 
 size_t find (char c, size_t pos = 0)  |   在原串中从pos位置往后找字符c首次出现的下标 pos可省略,从下标为0开始找  | 
代码案例:
void test()
{
	string s1("hollo world!!!");
	string s2{ "world" };
	int pos = s1.find(s2,1);//在原串中从下标为1的位置找s2,返回该位置的下标
	cout << pos << endl;
	char s3[] = "!!!";
	pos = s1.find(s3, 2);//在原串中从小标为2的位置找字符串s3的位置,返回下标
	cout << pos << endl;
	pos = s1.find("world", 1, 2);//在原串中从下标为1的位置,找字符串“world”中的n个字符出现的位置
	cout << pos << endl;
	pos = s1.find('o', 2);//在原串中从pos位置找字符‘o’首次出现的位置
	cout << pos << endl;
} 
 
std::string::rfind
使用形式跟find 一样,参数都一样的,唯一的区别是find是从前往后找,rfind是从后往前找

std::string::substr
 string substr (size_t pos = 0, size_t len = npos) const;  |   从pos位置开始截取len个字符长度,再返回 len可以没有,就从pos开始一直往后截取  | 
注意:这里是返回截取后的字符串,而不是在原串上更改
代码案例:
void test()
{
	string s1("hello world!!!");
	string s2 = s1.substr(6);//从下标为6的位置开始截取,一直到截完为止
	cout << "s2:" << s2 << endl;
	string s3 = s1.substr(6, 8);//从下标为6的位置开始截取8个字符
	cout << "s3:" << s3 << endl;
} 

如果发现有什么问题或建议欢迎提出!!!
谢谢阅读!!!



















