为什么空 unordered_map.find 不返回 end()?
Why is empty unordered_map.find not returning end()?
我正在尝试测试给定键是否已经存在unordered_map中的条目。我正在使用 find(( 函数返回一个迭代器。问题是,无论unordered_map是否为空,当给定键没有条目时,它都会返回一个"空"对象,如下所示 -
it = (<Error reading characters of string.>, -842150451)
我尝试创建一个空的项目解决方案并放置一个最小示例。它仍然为 find(( 返回相同的"空"对象。我什至尝试将键的类型从 std::string 更改为 int,结果相同。
#include <unordered_map>
int main(int argc, char* argv[])
{
std::string key = "test";
std::unordered_map<std::string, int> myMap;
myMap["abc"] = 5;
std::unordered_map<std::string, int>::iterator it = myMap.find(key);
}
我希望 find(( 在没有给定键的条目时返回"end",但输出是某种"空"对象。
只需将迭代器与列表末尾进行比较,例如
if (it == myMap.end()) {
std::cout << "didn't find" << std::endl;
} else {
std::cout << "found " << it->second << std::endl;
}
相关文章:
- 来自 std::list 的迭代器 .end() 按预期返回"0xcdcdcdcdcdcdcdcd"但 .begin()
- 什么时候在C++中返回常量引用是个好主意
- std::map<struct,struct>::find 找不到匹配项,但是如果我循环通过 begin() 到 end(),我在那里看到匹配项
- 如何使用 SFINAE 从 end() 方法返回 (const_) 迭代器
- 为什么空 unordered_map.find 不返回 end()?
- std::list.end() 不返回"past-the-end"迭代器
- 完全从c 中从std :: set.end()返回的内容
- 迭代器:如果 '*' 返回元素的引用,为什么删除 *(myList.end()) 会给出 SegFault
- 从end()函数返回的迭代器中获取BST的最后一个节点
- 集合数据结构中的 end() 迭代器应该返回什么
- 容器中的 begin() 和 end() 方法应始终具有相同的返回类型
- 我可以专门使用 std::begin 和 std::end 来获取 equal_range() 的返回值吗?
- 使用 std::vector 作为 std::map 的键在未找到和使用 reserve() 时不会返回 end()
- std::find返回vec.end(),而不管值是否在vector中
- 为我的向量包装器和迭代器返回'begin'和'end'函数返回什么?
- Qt容器(QVector,QList)::end()返回一个无法引用的迭代器
- 为什么stl_tree.h中的end()返回对迭代器对象的引用,而begin()返回object ?
- 对于哪个标准容器(如果有的话),end()返回的迭代器是持久化的
- 多个返回语句或"goto end;"
- 如果未找到映射条目,则返回特定的映射条目而不是 map::end