1使用例子
#include <iostream>
#include <map>
#include <string>
int main() {
// 创建一个从string到int的map
std::map<std::string, int> ageMap;
// 插入元素
ageMap["Alice"] = 25;
ageMap["Bob"] = 30;
ageMap.insert({"Charlie", 28});
// 遍历map
std::cout << "Map contents:" << std::endl;
for (const auto& pair : ageMap) {
std::cout << pair.first << ": " << pair.second << std::endl;
}
// 查找元素
auto it = ageMap.find("Bob");
if (it != ageMap.end()) {
std::cout << "Found Bob, age: " << it->second << std::endl;
}
// 删除元素
ageMap.erase("Alice");
// 检查大小
std::cout << "Map size after erase: " << ageMap.size() << std::endl;
return 0;
}
2底层实现细节:简化版红黑树实现