Files
XCSDD/docs/api/containers/string/reserve-resize.md
ssdfasd 58a83f445a fix: improve doc link navigation and tree display
- Fix link resolution with proper relative/absolute path handling
- Improve link styling with underline decoration
- Hide leaf nodes from tree, only show directories
- Fix log file path for packaged app
2026-03-19 12:44:08 +08:00

52 lines
1.3 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# String::Reserve / Resize
```cpp
void Reserve(SizeType capacity);
void Resize(SizeType newSize);
void Resize(SizeType newSize, char fillChar);
```
管理字符串的内存和大小。
**参数:**
- `capacity` - 要预留的最小容量
- `newSize` - 新的字符串长度
- `fillChar` - 当扩展字符串时用于填充新增位置的字符,默认为 '\0'
**返回:**
**复杂度:**
- `Reserve`:最坏 O(n),仅在需要扩展容量时复制数据
- `Resize`O(n),当 newSize > Length 时可能需要扩展
**示例:**
```cpp
#include "XCEngine/Containers/String.h"
#include <iostream>
int main() {
XCEngine::Containers::String s("Hi");
// Reserve - 预分配内存
s.Reserve(100);
std::cout << "After Reserve(100), Capacity: " << s.Capacity() << std::endl;
// 输出: After Reserve(100), Capacity: 100
// Resize - 缩短字符串
s.Resize(1);
std::cout << "After Resize(1): " << s.CStr() << std::endl; // 输出: H
// Resize - 扩展字符串,用 'X' 填充
s.Resize(5, 'X');
std::cout << "After Resize(5, 'X'): " << s.CStr() << std::endl; // 输出: HXXXX
return 0;
}
```
## 相关文档
- [String 总览](string.md) - 返回类总览
- [Length / Capacity](size.md) - 获取长度和容量
- [Clear](clear.md) - 清空字符串