- 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
40 lines
938 B
Markdown
40 lines
938 B
Markdown
# Array::Reserve()
|
|
|
|
```cpp
|
|
void Reserve(size_t capacity);
|
|
```
|
|
|
|
预分配底层内存容量,确保能容纳至少 `capacity` 个元素而不重新分配。
|
|
|
|
**行为:**
|
|
- 如果 `capacity > Capacity()`,分配新的内存(容量翻倍策略)
|
|
- 如果 `capacity <= Capacity()`,什么都不做
|
|
- 不改变 `Size()`
|
|
|
|
**参数:**
|
|
- `capacity` - 目标容量
|
|
|
|
**用途:** 当已知大致元素数量时,提前分配可以避免多次重新分配带来的性能开销和迭代器失效。
|
|
|
|
**复杂度:** O(n),其中 n 为当前元素数量(需要拷贝现有元素到新内存)
|
|
|
|
**线程安全:** ❌ 操作期间不可并发访问
|
|
|
|
**示例:**
|
|
|
|
```cpp
|
|
Containers::Array<int> arr;
|
|
|
|
// 预分配 1000 个元素的容量
|
|
arr.Reserve(1000);
|
|
|
|
// 之后添加 500 个元素不会触发重新分配
|
|
for (int i = 0; i < 500; ++i) {
|
|
arr.PushBack(i);
|
|
}
|
|
```
|
|
|
|
## 相关文档
|
|
|
|
- [Array 总览](array.md) - 返回类总览
|