如何在C ++中的2D哈希表中查找值

时间:2018-07-26 14:16:14

标签: c++ hashmap iterator hashtable

我有这个2d hashatable:

map<char, map<string, string>> hashtable;

我想检查以下值:hashtable ['a'] [“ Sa”]是否存在。怎么做?

谢谢。

1 个答案:

答案 0 :(得分:0)

首先,您必须检查hashtable['a']是否存在,然后检索地图的第二个元素,然后检查下一个元素。

auto it = hashtable.find('a');
if (it != hashtable.end()) {
    auto it2 = it->second.find("Sa");
    if(it2 != it->second.end()) {
        return true;
    }
    return false;
}
return false;

如果元素不存在,则方法find()返回一个等于映射末尾的迭代器。