上一篇博文内容:string类的构造,拷贝,赋值拷贝,及其模拟实现[ C++ ] string类之构造,拷贝,赋值 及其模拟实现
本篇内容:string类的常见接口及其模拟实现,我将会从遍历,增,删,查,改5个方面对常见,常用的接口进行模拟实现string类。
该思维导图是本篇博文主要内容
![[ C++ ] string类常见接口及其模拟实现_迭代器](https://s2.51cto.com/images/blog/202208/25155721_63072b61b6cdc74641.png?x-oss-process=image/watermark,size_14,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_30,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=,x-oss-process=image/resize,m_fixed,w_1184)
![[ C++ ] string类常见接口及其模拟实现_代码实现_02](https://s2.51cto.com/images/202208/52647ed8304d232b563713e6002a1b389d9f4b.gif)
1、遍历
1.1 下标+operator [ ]
这种方法是最好理解的,我们使用下标将字符串的内容逐字符输出。
char& operator[](size_t pos)
{
assert(pos < strlen(_str));
return _str[pos];
}
const char& operator[](size_t pos) const
{
assert(pos < strlen(_str));
return _str[pos];
}
int main(){
string s1("hello world");
for (int i = 0; i < s1.size(); ++i)
{
cout << s1[i];
}
return 0;
}
![[ C++ ] string类常见接口及其模拟实现_迭代器_03](https://s2.51cto.com/images/202208/d17f6755461924679e949784f49a6616e50629.gif)
![[ C++ ] string类常见接口及其模拟实现_迭代器_04](https://s2.51cto.com/images/blog/202208/25155721_63072b61d6f9e5359.png?x-oss-process=image/watermark,size_14,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_30,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=,x-oss-process=image/resize,m_fixed,w_1184)
![[ C++ ] string类常见接口及其模拟实现_代码实现_05](https://s2.51cto.com/images/202208/c8e04912800fee42953451a5796411892153e4.gif)
1.2 c_str
![[ C++ ] string类常见接口及其模拟实现_字符串_06](https://s2.51cto.com/images/blog/202208/25155721_63072b61eaaf264987.png?x-oss-process=image/watermark,size_14,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_30,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=,x-oss-process=image/resize,m_fixed,w_1184)
![[ C++ ] string类常见接口及其模拟实现_字符串_07](https://s2.51cto.com/images/202208/22c35bf68cc103f9126561b917ecdff80c9d88.gif)
我们发现c_str返回的就是该字符串以及末尾的'\0',我们可以使用c_str()来进行字符串的打印,遇到'\0'就停止。
实现代码:
const char* c_str() const{
return
![[ C++ ] string类常见接口及其模拟实现_代码实现_08](https://s2.51cto.com/images/202208/b29e44908739513dbbf786527a7cdee1235b2f.gif)
![[ C++ ] string类常见接口及其模拟实现_代码实现_09](https://s2.51cto.com/images/blog/202208/25155721_63072b61b85a218576.png?x-oss-process=image/watermark,size_14,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_30,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=,x-oss-process=image/resize,m_fixed,w_1184)
![[ C++ ] string类常见接口及其模拟实现_字符串_10](https://s2.51cto.com/images/202208/c176b63244725b989ab696f8893a780955d99c.gif)
1.3 迭代器
迭代器:字符串的迭代器底层是一个char* 的原生指针,因此我们使用string的迭代器时就像指针一样使用即可,但是其他的容器底层实现并不一定都是原生指针。
typedef char* iterator;
typedef const char* const_iterator;
iterator begin(){
return _str;
}
iterator begin() const{
return _str;
}
iterator end(){
return _str + _size;
}
iterator end() const{
return
![[ C++ ] string类常见接口及其模拟实现_迭代器_11](https://s2.51cto.com/images/202208/546366e5136944f799627199dbc3983cadbc01.gif)
我们使用迭代器来遍历一下字符串:
int main(){
string s1("hello world");
string::iterator it = s1.begin();
while (it != s1.end())
{
cout << *it << " ";
it++;
}
return 0;
}
![[ C++ ] string类常见接口及其模拟实现_迭代器_12](https://s2.51cto.com/images/202208/168d26487743bc76233280d4be9de55983413b.gif)
![[ C++ ] string类常见接口及其模拟实现_代码实现_13](https://s2.51cto.com/images/blog/202208/25155721_63072b61c201119451.png?x-oss-process=image/watermark,size_14,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_30,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=,x-oss-process=image/resize,m_fixed,w_1184)
![[ C++ ] string类常见接口及其模拟实现_代码实现_14](https://s2.51cto.com/images/202208/68df7150797e42c775148287e433308d98b442.gif)
1.4 范围for
范围for的使用
for (auto e : s1)
{
cout << e << " ";
}
![[ C++ ] string类常见接口及其模拟实现_代码实现_15](https://s2.51cto.com/images/202208/5652f70373345ab5aee209c9a075e3de9d63d4.gif)
![[ C++ ] string类常见接口及其模拟实现_代码实现_16](https://s2.51cto.com/images/blog/202208/25155721_63072b61c416198642.png?x-oss-process=image/watermark,size_14,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_30,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=,x-oss-process=image/resize,m_fixed,w_1184)
![[ C++ ] string类常见接口及其模拟实现_字符串_17](https://s2.51cto.com/images/202208/e8db1c3283a1e18a40980958a55452170108ba.gif)
范围for的底层原理:范围for的底层原理其实就是迭代器。我们如何来验证呢,我们可以将自己实现的迭代器屏蔽起来,再使用范围for进行遍历发现就会出现报错。
![[ C++ ] string类常见接口及其模拟实现_迭代器_18](https://s2.51cto.com/images/blog/202208/25155722_63072b621319088252.png?x-oss-process=image/watermark,size_14,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_30,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=,x-oss-process=image/resize,m_fixed,w_1184)
![[ C++ ] string类常见接口及其模拟实现_字符串_19](https://s2.51cto.com/images/202208/f51702e25970e9758ef28389ac16c16e18fead.gif)
2、增
2.1.push_back
![[ C++ ] string类常见接口及其模拟实现_字符串_20](https://s2.51cto.com/images/blog/202208/25155721_63072b61d8a3d9686.png?x-oss-process=image/watermark,size_14,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_30,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=,x-oss-process=image/resize,m_fixed,w_1184)
![[ C++ ] string类常见接口及其模拟实现_字符串_21](https://s2.51cto.com/images/202208/110969d59b69541a0a398476f55b09de1ea0c4.gif)
通过查询文档,我们知道了push_back的作用是将一个字符添加到目标字符串后。
方法:
1、首先我们肯定要判断是否需要扩容。如果_size == _capacity ,说明满了,需要扩容。
扩容的方法:开辟一个2倍的新空间,将字符串拷贝到新空间,现有的空间释放掉即可。
2、扩容完毕后我们在下标为_size的位置加入新增字符。
3、最后我们将_size++,并处理'\0'即可。
实现代码:
void push_back(char{
if (_size == _capacity)
{
char* tmp = new char[_capacity * 2 + 1];
strcpy(tmp, _str);
delete[] _str;
_str = tmp;
_capacity *= 2;
//reserve(_capacity == 0?4:_capacity*2);
}
_str[_size] = ch;
++_size;
_str[_size] = '\0';
//insert(_size, ch);
![[ C++ ] string类常见接口及其模拟实现_代码实现_22](https://s2.51cto.com/images/202208/b5c6003084c56f1bc709223acc86d1051c9679.gif)
2.2. 重载 += (char ch)
我们在日常写代码时很少会使用push_back,我们经常会使用 += ,因此我们也有必要实现一下 +=,+= 的实现可以复用 push_back。
代码实现:
operator+=( char ch)
{
push_back(ch);
return *this;
}
![[ C++ ] string类常见接口及其模拟实现_代码实现_23](https://s2.51cto.com/images/202208/0239d2c362f3bd32dd7627d4edd96d9471376f.gif)
2.3. append
![[ C++ ] string类常见接口及其模拟实现_字符串_24](https://s2.51cto.com/images/blog/202208/25155721_63072b61e952f38107.png?x-oss-process=image/watermark,size_14,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_30,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=,x-oss-process=image/resize,m_fixed,w_1184)
![[ C++ ] string类常见接口及其模拟实现_字符串_25](https://s2.51cto.com/images/202208/d4f12cf4410f4a2057b6919cc1fd898f76aa0a.gif)
前两个接口针对新增一个字符,append接口是将一个字符串新增到源字符串后。
方法:
1、首先,我们依然要考虑源字符串是否需要扩容,这次的扩容和push_back的扩容仍有区别,push_back由于只新增一个字符,因此在源字符串大小的基础上扩容2倍是足够的;append如果也是在源字符串的大小上扩容2倍的话并不能保证空间足够,因为并不知道新字符串的大小。那么确保新空间的大小一定足够呢?因此我们只需要计算源字符串的_size + 新字符串的_size的和,这个和就是新空间的大小。这也一定能确保新空间足够。因此我们可以使用reserve接口(该接口实现在 5.1)
2、只需要修改_size.
代码实现:
void append(const char* str){
size_t len = _size + strlen(str);
if (len > _capacity)
{
reserve(len);
}
strcpy(_str + len, str);
_size = len;
//insert(_size, str);
![[ C++ ] string类常见接口及其模拟实现_字符串_26](https://s2.51cto.com/images/202208/a7cbae293b06bced5ec189045ab1de5abeb4f0.gif)
2.4.重载 += (char* str)
+=字符串实现原理也是直接复用append
代码实现:
operator+=(const char* str)
{
append(str);
return *this;
}
![[ C++ ] string类常见接口及其模拟实现_迭代器_27](https://s2.51cto.com/images/202208/481f9bd31a570659d151257d9995309ebe6eae.gif)
2.5. insert (任意位置插入)
![[ C++ ] string类常见接口及其模拟实现_字符串_28](https://s2.51cto.com/images/blog/202208/25155722_63072b620316597757.png?x-oss-process=image/watermark,size_14,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_30,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=,x-oss-process=image/resize,m_fixed,w_1184)
![[ C++ ] string类常见接口及其模拟实现_字符串_29](https://s2.51cto.com/images/202208/d923cc85525afcb51836435714f2fd67e6e615.gif)
![[ C++ ] string类常见接口及其模拟实现_代码实现_30](https://s2.51cto.com/images/blog/202208/25155721_63072b61dc26595294.png?x-oss-process=image/watermark,size_14,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_30,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=,x-oss-process=image/resize,m_fixed,w_1184)
![[ C++ ] string类常见接口及其模拟实现_字符串_31](https://s2.51cto.com/images/202208/446c6a9240e1afc5e8603011450e842e163da3.gif)
2.5.1 任意位置插入一个字符
方法:
1、首先:判断pos位置的合法性。
2、其次:判断是否需要扩容,如果满了则需要扩容(2倍扩即可)
3、再者:从_size+1位置开始从后往前分别将每个字符向后移动一位,当end等于pos位置时,移动结束
4、最后:在pos位置插入新字符,修改_size
![[ C++ ] string类常见接口及其模拟实现_迭代器_32](https://s2.51cto.com/images/blog/202208/25155721_63072b61e33428260.png?x-oss-process=image/watermark,size_14,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_30,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=,x-oss-process=image/resize,m_fixed,w_1184)
![[ C++ ] string类常见接口及其模拟实现_字符串_33](https://s2.51cto.com/images/202208/61858b94212d4bc354951671e8375bc3a175ce.gif)
代码实现如下:
string& insert(size_t pos, char{
assert(pos <= _size);
//判断size
if (_size == _capacity)
{
reserve(_capacity == 0 ? 4 : _capacity * 2);
}
size_t end = _size+1;
while (end > pos)
{
_str[end] = _str[end-1];
--end;
}
_str[pos] = ch;
++_size;
return *this;
}
![[ C++ ] string类常见接口及其模拟实现_字符串_34](https://s2.51cto.com/images/202208/321d42f0135ca807ce0886ae93dce890e31204.gif)
注:当pos == size时,相当于push_back,因此push_back可以复用insert。
2.5.2 任意位置插入一个字符串
任意位置插入一个字符串时原理基本和插入一个字符相同,需要注意临界条件即可。
![[ C++ ] string类常见接口及其模拟实现_迭代器_35](https://s2.51cto.com/images/blog/202208/25155721_63072b61ef47085094.png?x-oss-process=image/watermark,size_14,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_30,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=,x-oss-process=image/resize,m_fixed,w_1184)
![[ C++ ] string类常见接口及其模拟实现_迭代器_36](https://s2.51cto.com/images/202208/830fc4a97fc8a2de874390168120a2550e6b3b.gif)
方法:
1、首先:判断pos位置的合法性。
2、其次:判断是否需要扩容,如果满了则需要扩容,这里的扩容和append的扩容原理相同,使用reserve扩。
3、再者:挪动数据。 依然是从后往前挪。此时end = size + len(len = strlen(str)),依次将end - len位置的字符挪动到end位置,每挪一个字符,end--,当 end == pos+len - 1时候说明挪动结束。
4、最后,插入字符串,修改_size即可。
string& insert(size_t pos, const char* str){
assert(pos <= _size);
size_t len = strlen(str);
if (len == 0)
{
return *this;
}
if (_size + len > _capacity)
{
reserve(_size + len);
}
//挪动数据
size_t end = _size + len;
//while(end >= end+len)
while (end > pos+len-1)
{
_str[end] = _str[end-len];
--end;
}
//插入数据
size_t i = 0;
while (i < len)
{
_str[pos+i] = str[i];
++i;
}
_size += len;
return *this;
}
![[ C++ ] string类常见接口及其模拟实现_代码实现_37](https://s2.51cto.com/images/202208/34a330286c4dac8c7e94445e5764c0a9840090.gif)
3.删
3.1 earse
![[ C++ ] string类常见接口及其模拟实现_迭代器_38](https://s2.51cto.com/images/blog/202208/25155721_63072b61ee74848743.png?x-oss-process=image/watermark,size_14,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_30,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=,x-oss-process=image/resize,m_fixed,w_1184)
![[ C++ ] string类常见接口及其模拟实现_迭代器_39](https://s2.51cto.com/images/202208/c720d8383884f67dafc11082a21693b059ade6.gif)
库中提到了一个npos,我们可以看一下npos到底是什么?
![[ C++ ] string类常见接口及其模拟实现_代码实现_40](https://s2.51cto.com/images/blog/202208/25155721_63072b61da1f9645.png?x-oss-process=image/watermark,size_14,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_30,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=,x-oss-process=image/resize,m_fixed,w_1184)
![[ C++ ] string类常见接口及其模拟实现_迭代器_41](https://s2.51cto.com/images/202208/5257bec3970e3d663e6139a5a75f1c0ee4008a.gif)
npos的大小肯定是大于给定len的大小,因此就是有多少删多少。
通过库里面的定义,erase的作用是在pos位置往后删除len个字符
其中erase删除可分为这几种情况:
情况1:如果len == npos,或者pos + len >= _size,说明要删完,后面删完的话只需要在pos位置赋成 '\0' 即可。
![[ C++ ] string类常见接口及其模拟实现_迭代器_42](https://s2.51cto.com/images/blog/202208/25155721_63072b61e5ab251022.png?x-oss-process=image/watermark,size_14,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_30,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=,x-oss-process=image/resize,m_fixed,w_1184)
![[ C++ ] string类常见接口及其模拟实现_代码实现_43](https://s2.51cto.com/images/202208/c3682c371cd6c0e7001617c91d24addc3b138a.gif)
情况2:没有删完,删除一部分,我们可以定义一个begin = pos + len,让begin 位置的字符赋值给begin - len 位置的字符,当begin == _size时,就自动将 ' \0 ' 赋值过去了,因此结束的条件是begin > _size。 最后再处理一下 _size -= len
![[ C++ ] string类常见接口及其模拟实现_字符串_44](https://s2.51cto.com/images/blog/202208/25155721_63072b61e465442816.png?x-oss-process=image/watermark,size_14,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_30,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=,x-oss-process=image/resize,m_fixed,w_1184)
![[ C++ ] string类常见接口及其模拟实现_代码实现_45](https://s2.51cto.com/images/202208/521f7fb1456c18bbc83908d54d8f41334e5e82.gif)
代码实现:
string& earse(size_t pos, size_t{
assert(pos < _size);
if (len == npos || pos + len >= _size)
{
_str[pos] = '\0';
_size = pos;
}
else
{
size_t begin = pos + len;
while (begin <= _size)
{
_str[begin-len] = _str[begin];
++begin;
}
_size -= len;
}
return *this;
}
![[ C++ ] string类常见接口及其模拟实现_代码实现_46](https://s2.51cto.com/images/202208/15da58c02e3915f8b266294bf56dfb78f6b1f9.gif)
4.查
4.1 查找一个字符
![[ C++ ] string类常见接口及其模拟实现_字符串_47](https://s2.51cto.com/images/blog/202208/25155721_63072b61f1f7122554.png?x-oss-process=image/watermark,size_14,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_30,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=,x-oss-process=image/resize,m_fixed,w_1184)
![[ C++ ] string类常见接口及其模拟实现_代码实现_48](https://s2.51cto.com/images/202208/3926bb1509ebea2b09e106b3a335a165baad19.gif)
find查字符串返回第一次出现该字符的下标,这个实现也比较简单。
size_t find(char ch,size_t pos = 0){
for (; pos < _size; ++pos)
{
if (_str[pos] == ch)
{
return pos;
}
}
return
![[ C++ ] string类常见接口及其模拟实现_迭代器_49](https://s2.51cto.com/images/202208/d85df1d522cb4cebb2a31201a516c7decaeb61.gif)
4.2 查找一个字符串
查找子串可以使用C语言中的strstr
size_t find(const char* str, size_t pos = 0){
const char* p = strstr(_str + pos, str);
//kmp算法 -- 效率不好 不实用
//bm算法
if (p == nullptr)
{
return npos;
}
else
{
return
![[ C++ ] string类常见接口及其模拟实现_字符串_50](https://s2.51cto.com/images/202208/b34c584585273adbae62935606faf9e7902603.gif)
4.3 字符串比较
这里我们比较的是字符的ASCII码值的大小,可以使用strcmp
bool operator<(const string& s1, const string& s2)
{
return strcmp(s1.c_str(), s2.c_str()) < 0;
}
bool operator==(const string& s1, const string& s2)
{
return strcmp(s1.c_str(), s2.c_str()) == 0;
}
bool operator<=(const string& s1, const string& s2)
{
return s1 < s2 || s1 == s2;
}
bool operator>(const string& s1, const string& s2)
{
return !(s1 <= s2);
}
bool operator>=(const string& s1, const string& s2)
{
return !(s1 < s2);
}
bool operator!=(const string& s1, const string& s2)
{
return
![[ C++ ] string类常见接口及其模拟实现_字符串_51](https://s2.51cto.com/images/202208/17ba37a29bfec4a56569504ae9ce7ec474df7d.gif)
5.改
5.1 reserve
![[ C++ ] string类常见接口及其模拟实现_迭代器_52](https://s2.51cto.com/images/blog/202208/25155722_63072b62112e52920.png?x-oss-process=image/watermark,size_14,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_30,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=,x-oss-process=image/resize,m_fixed,w_1184)
![[ C++ ] string类常见接口及其模拟实现_代码实现_53](https://s2.51cto.com/images/202208/36785f711d296a6533d3597049bdafe917fdcd.gif)
reserve作用就是将string的容量扩充到n个。
方法:首先需要判断n是否大于_capacity,如果n小于等于_capacity则不进行容量修改,如果n大于_capacity,则需要进行扩充到n。
代码实现:
void reserve(size_t{
if (n > _capacity)
{
char* tmp = new char[n + 1];
strcpy(tmp, _str);
delete[] _str;
_str = tmp;
_capacity = n;
}
}
![[ C++ ] string类常见接口及其模拟实现_字符串_54](https://s2.51cto.com/images/202208/c4c9b074581ab1e9957171f628f0663c000a52.gif)
5.2 resize
![[ C++ ] string类常见接口及其模拟实现_代码实现_55](https://s2.51cto.com/images/blog/202208/25155722_63072b620af8452877.png?x-oss-process=image/watermark,size_14,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_30,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=,x-oss-process=image/resize,m_fixed,w_1184)
![[ C++ ] string类常见接口及其模拟实现_字符串_56](https://s2.51cto.com/images/202208/9596860992591341786181cc78f34c915a5609.gif)
resize的作用是将字符串的大小调整为n个字符。resize修改的是size。
resize和reserve的区别:reserve只对空间进行处理,不影响值;resize不仅会改变空间,还会改变size,会改变字符的值。
情况1: resize的n大于当前字符串的长度。
用如图举例,s1中存放 hello world,大小只有15个空间,现在要resize(20,'x'),此时空间不够,需要扩容,开辟新空间后将源字符串内容拷贝下来并在后面加满 'x'
![[ C++ ] string类常见接口及其模拟实现_迭代器_57](https://s2.51cto.com/images/blog/202208/25155721_63072b61eac8290856.png?x-oss-process=image/watermark,size_14,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_30,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=,x-oss-process=image/resize,m_fixed,w_1184)
![[ C++ ] string类常见接口及其模拟实现_代码实现_58](https://s2.51cto.com/images/202208/48434af01cfaa39f1c3819421f4f21dc23f3bb.gif)
![[ C++ ] string类常见接口及其模拟实现_字符串_59](https://s2.51cto.com/images/blog/202208/25155722_63072b620931399485.png?x-oss-process=image/watermark,size_14,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_30,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=,x-oss-process=image/resize,m_fixed,w_1184)
![[ C++ ] string类常见接口及其模拟实现_字符串_60](https://s2.51cto.com/images/202208/b905ae19415aac698cf112572a2cdb25e2c171.gif)
情况2: resize的n小于当前字符串_capacity的长度。但是大于字符串_size的长度
如下图所示,在这种情况下,s1的_capacity不会变,而是将_size增加到n,并在字符串后弥补目标字符。
![[ C++ ] string类常见接口及其模拟实现_代码实现_61](https://s2.51cto.com/images/blog/202208/25155721_63072b61e4d0c61439.png?x-oss-process=image/watermark,size_14,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_30,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=,x-oss-process=image/resize,m_fixed,w_1184)
![[ C++ ] string类常见接口及其模拟实现_字符串_62](https://s2.51cto.com/images/202208/f49bcd700044a015bdf235e3b0496222aaf421.gif)
![[ C++ ] string类常见接口及其模拟实现_代码实现_63](https://s2.51cto.com/images/blog/202208/25155722_63072b620480c12734.png?x-oss-process=image/watermark,size_14,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_30,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=,x-oss-process=image/resize,m_fixed,w_1184)
![[ C++ ] string类常见接口及其模拟实现_字符串_64](https://s2.51cto.com/images/202208/a6cae2f602651202621477b39fb996021c4ed5.gif)
情况3: resize的n小于当前字符串_resize
如下图所示,在这种情况下,s1的_size修改为目标大小n,并改变值,只会保留前n个字符的内容
![[ C++ ] string类常见接口及其模拟实现_字符串_65](https://s2.51cto.com/images/blog/202208/25155722_63072b62066ab16630.png?x-oss-process=image/watermark,size_14,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_30,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=,x-oss-process=image/resize,m_fixed,w_1184)
![[ C++ ] string类常见接口及其模拟实现_迭代器_66](https://s2.51cto.com/images/202208/f71641731928371db52922a2372431c886aca2.gif)
在了解resize的作用后,我们也进行了不同情况的分类,模拟实现代码如下:
//扩空间+初始化
//删除部分数据,保留前n个
void resize(size_t n,char ch = '\0'){
if (n < _size)
{
_size = n;
_str[_size] = '\0';
}
else
{
if (n > _capacity)
{
reserve(n);
}
for (size_t i = _size; i < n; ++i)
{
_str[i] = ch;
}
_size = n;
_str[_size] = '\0';
}
}
![[ C++ ] string类常见接口及其模拟实现_字符串_67](https://s2.51cto.com/images/202208/e279c8663b63b5e5e244022ed63f20fb9314ec.gif)
(本篇完)