c++STL库unordered_map
(2024-10-04 19:23:52)分类: 接口类的哈 |
#include
#include
#include
// 创建一个
unordered_map
std::unordered_map
wordCount;
// 插入元素
wordCount["apple"] =
1;
wordCount["banana"] =
2;
// 使用 insert
方法插入键值对
wordCount.insert({
"orange", 3 });
// 访问元素
std::cout <<
"Count for 'apple': " << wordCount["apple"] <<
std::endl;
// 检查键是否存在
if
(wordCount.find("banana") != wordCount.end()) {
std::cout << "'banana' is in the map."
<< std::endl;
}
// 遍历
unordered_map
std::cout << "All
elements in unordered_map:" << std::endl;
for (const auto&
pair : wordCount) {
std::cout << pair.first << ": "
<< pair.second << std::endl;
}
// 修改元素
wordCount["apple"] =
5;
// 删除元素
wordCount.erase("orange");
return 0;
int main() {
}
后一篇:2024年10月04日