Files
XCEngine/docs/api/containers/hashmap/contains.md

40 lines
778 B
Markdown

# HashMap::Contains
```cpp
bool Contains(const Key& key) const;
```
检查哈希表中是否包含指定的键。
**参数:**
- `key` - 要检查的键
**返回:** 如果键存在返回 `true`,否则返回 `false`
**复杂度:** O(1) 平均,最坏 O(n)
**线程安全:** ❌ 非线程安全
**异常:**
**示例:**
```cpp
XCEngine::Containers::HashMap<int, const char*> map;
map.Insert(1, "one");
map.Insert(2, "two");
if (map.Contains(1)) {
std::cout << "Key 1 exists" << std::endl; // 输出 "Key 1 exists"
}
if (!map.Contains(99)) {
std::cout << "Key 99 does not exist" << std::endl; // 输出 "Key 99 does not exist"
}
```
## 相关文档
- [HashMap 总览](hashmap.md) - 返回类总览
- [Find](find.md) - 查找键对应的值