- 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
34 lines
929 B
Markdown
34 lines
929 B
Markdown
# HashMap::operator[]
|
||
|
||
```cpp
|
||
Value& operator[](const Key& key);
|
||
```
|
||
|
||
按下标访问键对应的值。如果键不存在,则插入一个默认构造的值并返回引用。
|
||
|
||
**参数:**
|
||
- `key` - 要访问的键
|
||
|
||
**返回:** 对应值的引用。如果键不存在,则返回一个默认构造的 `Value` 的引用。
|
||
|
||
**复杂度:** O(1) 平均,最坏 O(n)(发生 rehash 时)
|
||
|
||
**示例:**
|
||
|
||
```cpp
|
||
XCEngine::Containers::HashMap<int, std::string> map;
|
||
|
||
map[1] = "one"; // 插入键 1,值 "one"
|
||
map[2] = "two"; // 插入键 2,值 "two"
|
||
|
||
std::string& value = map[1]; // 获取键 1 对应的值,结果为 "one"
|
||
|
||
map[3]; // 插入键 3,值为 std::string 的默认构造值
|
||
```
|
||
|
||
## 相关文档
|
||
|
||
- [HashMap 总览](hashmap.md) - 返回类总览
|
||
- [Find](find.md) - 查找键对应的值(不插入)
|
||
- [Insert](insert.md) - 插入键值对(不覆盖已存在的键)
|