【发布时间】:2019-03-29 15:58:18
【问题描述】:
我正在尝试更新嵌套映射中的键(如果存在)或插入它(如果不存在)。我正在尝试使用带有 lower_bound 的迭代器来提高这个过程的效率。
std::unordered_map<std::string, std::unordered_map<std::string, std::string>> maps;
cache::iterator iter(maps[command[1]].lower_bound(command[2]));
if (iter == maps[command[1]].end() || command[2] < iter->first) {
maps[command[1]].insert(iter, std::make_pair(command[2], command[3]));
} else {
iter->second = command[3];
}
我收到以下编译时错误:
no member named 'lower_bound' in 'std::unordered_map<std::basic_string<char>, std::basic_string<char>, std::hash<std::string>, std::equal_to<std::basic_string<char> >, std::allocator<std::pair<const std::basic_string<char>, std::basic_string<char> > > >'
【问题讨论】:
-
那是因为
std::unordered_map::lower_bound不存在!在元素未排序的容器上寻找下限是没有意义的。 -
那么有没有有效的方法来解决这个问题?
-
使用
find查看元素是否存在有什么问题? -
插入提示在基于散列的容器上并不是真正有用(除非新元素散列到与提示指向的元素相同,这基本上意味着它是同一个元素,在在这种情况下你无论如何都不会插入)。
标签: c++ nested unordered-map