Files
XCSDD/docs/api/containers/hashmap/operator-subscript.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

929 B
Raw Permalink Blame History

HashMap::operator[]

Value& operator[](const Key& key);

按下标访问键对应的值。如果键不存在,则插入一个默认构造的值并返回引用。

参数:

  • key - 要访问的键

返回: 对应值的引用。如果键不存在,则返回一个默认构造的 Value 的引用。

复杂度: O(1) 平均,最坏 O(n)(发生 rehash 时)

示例:

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 总览 - 返回类总览
  • Find - 查找键对应的值(不插入)
  • Insert - 插入键值对(不覆盖已存在的键)