目录
[5.与C语言的配合的接口函数: c_str](#5.与C语言的配合的接口函数: c_str)
[练习题: 字符串最后一个单词的长度](#练习题: 字符串最后一个单词的长度)
1.reserve函数(不是reverse)

https://legacy.cplusplus.com/reference/string/string/reserve/

如果n>capacity,那么reverse函数会改变capacity
作用:请求改变容量capacity
代码示例
cpp
#include <iostream>
#include <string>
using namespace std;
int main()
{
string str("teststring");
cout << "reserve前:"<<str.capacity() << endl;
str.reserve(20);
cout << "reserve后:"<< str.capacity() << endl;
return 0;
}
运行结果:实际上可能多开一点,因为要考虑内存对齐,也有可能开的空间刚刚好

优点:写reserve后,插入少量字符时后面不需要扩容,避免异地扩容消耗时间
有可能reserve的参数大小比原字符串的大小要小,但有字符串的情况下reserve不会缩容,且不会改变size

如果没有字符串的情况下(即空串),可能会缩容,也可能什么都不做,具体和STL的实现有关(因为没有规定要缩容,而且缩容是释放原空间,开辟新的小空间,用时间换空间,代价较大,因此不建议使用与缩容有关的函数)
如果reserve()比capacity小,不同平台的反应不同
cpp
#include <iostream>
#include <string>
using namespace std;
int main()
{
string str("teststring");
cout << "reserve前:"<<str.capacity() << endl;
str.reserve(1);
cout << "reserve后:"<< str.capacity() << endl;
return 0;
}
运行结果:
VS2022:

Ubuntu服务器:

2.resize
https://legacy.cplusplus.com/reference/string/string/resize/
resize字面意思理解:"re"+"size",即改变size
代码示例
cpp
#include <iostream>
#include <string>
using namespace std;
int main()
{
string str("teststring");
str.resize(4);
cout <<"str :"<< str << endl;
cout <<"str.size() :"<< str.size() << endl;
cout <<"str.capacity() :"<< str.capacity() << endl;
return 0;
}
运行结果: 字符串被截断处理

截断处理的策略:添加\0,指定字符串的结尾

当resize的参数小于字符串的size时,直接改变size,不会改变capacity
cpp
#include <iostream>
#include <string>
using namespace std;
int main()
{
string str("teststring");
cout << "str :" << str << endl;
cout << "str.capacity() :" << str.capacity() << endl;
cout << "str.size() :" << str.size() << endl;
str.resize(20);
cout <<"str :"<< str << endl;
cout <<"str.size() :"<< str.size() << endl;
cout <<"str.capacity() :"<< str.capacity() << endl;
return 0;
}
运行结果:VS2022平台下,capacity变了

cpp
#include <iostream>
#include <string>
using namespace std;
int main()
{
string str("teststring");
cout << "str :" << str << endl;
cout << "str.capacity() :" << str.capacity() << endl;
cout << "str.size() :" << str.size() << endl;
str.resize(13);
cout <<"str :"<< str << endl;
cout <<"str.size() :"<< str.size() << endl;
cout <<"str.capacity() :"<< str.capacity() << endl;
return 0;
}
运行结果:VS2022平台下,capacity没有变

当resize的参数大于字符串的size时,一定会改变size,有可能会改变capacity,具体情况和编译器的内存分配策略有关
3.reserve和resize的区别
cpp
#include <string>
using namespace std;
int main()
{
string str1;
str1.reserve(15);
string str2;
str2.resize(15);
return 0;
}
下断点到return 0,打开监视窗口:

区别:reserve只是扩容,而resize是扩容+填值初始化(默认填\0) resize会让size和capacity都变(size变的原因是填值初始化)
4.shrink_to_fit
https://legacy.cplusplus.com/reference/string/string/shrink_to_fit/
shrink v.收缩
shrink_to_fit:收缩至适合
作用:请求减少capacity,让其适合size大小(显然是缩容函数,不建议使用)
请求不一定会执行,具体看编译器的处理策略,即该函数会弹性处理,有可能不会正好是size!
代码示例
cpp
#include <iostream>
#include <string>
using namespace std;
int main()
{
string str("teststring");
str.reserve(100);//reserve只是扩容
cout << "capacity: " << str.capacity() << endl;
str.shrink_to_fit();
cout << "capacity: " << str.capacity() << endl;
return 0;
}
运行结果:

5.与C语言的配合的接口函数: c_str
https://legacy.cplusplus.com/reference/string/string/c_str/

作用:获得C语言版的只读 字符串,好处:一些C语言函数只支持C风格的字符串,提高C++对C语言的兼容性
(注:无论是C++98还是C++11,返回的字符串都是不可以修改的!!!!!!)
代码示例
cpp
#include <iostream>
#include <string>
#include <stdio.h>
using namespace std;
int main()
{
string str("teststring");
//注意是const char*,有const修饰
const char* p = str.c_str();
printf("%s", p);
return 0;
}
运行结果:

6.rfind
知识回顾:find函数
参见CC15.【C++ Cont】string类字符串的find和substr函数文章
rfind
https://legacy.cplusplus.com/reference/string/string/rfind/

和find相反,从string类字符串的后面向前面找
代码示例
cs
#include <iostream>
#include <string>
using namespace std;
int main()
{
string str("abcabcba");
cout << str.rfind("c");
return 0;
}
运行结果:


练习题: 字符串最后一个单词的长度
代码
cpp
#include <iostream>
#include <string>
using namespace std;
int main()
{
string str;
getline(cin,str);
size_t pos = str.rfind(" ");
if (pos == string::npos)
cout << str.size();
else
cout << str.size() - 1 - pos;
}
因为是"字符串最后一个单词的长度"则可以反向遍历,使用rfind函数,找空格即可,如果没有空格(pos==string::npos),返回str.size()即可
注意:本题不能使用cin读取字符串,cin在遇到空格会停止读取,必须用getline一次读一行
(cin和getline的对比详细参见CC19.【C++ Cont】scanf/printf 和 cin/cout的对比文章)