测试
当使用非键值的下标访问时,返回的结果
1.<指针, 指针>
Node* head = new Node(1);
std::unordered_map<Node*, Node*> map1;
if (map1[head] == nullptr)
std::cout << "返回空指针" << std::endl;
结果显示,返回空指针。
2.<int, int>
std::unordered_map<int, int> map2;
std::cout << map2[2] << std::endl;
结果显示,返回0
3.<int, 指针>
std::unordered_map<int, Node*> map3;
if (map3[2] == nullptr)
std::cout << "返回空指针" << std::endl;
结果显示,返回空指针。
4.<指针, int>
std::unordered_map<Node*, int> map4;
std::cout << map4[head] << std::endl;
结果显示,返回0
5.<指针, char>
std::unordered_map<Node*, char> map5;
if (map5[head] == '\0')
std::cout << "返回" << std::endl;
结果显示,返回’\0’
小结
对于<key, value>
非键值访问,当value为指针类型时,返回nullptr
非键值访问,当value为int类型时,返回0
非键值访问,当value为char类型时,返回’\0’
如有错误之处,还请不吝赐教。