Unordered_map::find()插入查找的键
unordered_map::find() inserts the key of the lookup
unordered_map::find()的一个特性是插入我们自动查找0值的键吗?我把这个写清楚
unordered_map<int, int> tempMap;
//some code
if(tempMap.find(1) == tempMap.end()){
//do something but not insert the 1 with a corresponding value into the tempMap
}
所以如果我再次查找1,它会在tempMap中以0作为对应值吗?这是unordered_map的特性吗?
不,find
只搜索并返回一个迭代器。
也就是说,std::unordered_map
(和std::map
)都重载operator[]
,如果需要,将插入默认值:
// if element with a key value one exists, return that. otherwise, insert
// a default initialized element (zero in this case), and return that
auto& val = tempMap[1];
No——find
not insert values.
如果你想插入之前不存在的值,那么你可以使用operator[]
而不是find
。
这样做是因为operator[]
返回一个对象的引用。由于不存在空引用这样的东西,本质上唯一的替代方法就是在搜索以前不存在的项时抛出异常。已经有一些容器采用了这种行为,但它显然不是很有用,也没有得到多少普及(我使用过一些这样的容器,发现它很痛苦)。
相关文章:
- 正在查找文档以获得PS4平台的C++中的设备信息
- 使用C++库在Android项目中修改gradle中的cmake参数,用于插入指令的测试
- 有关插入适配器的错误。[错误]请求从 'back_insert_iterator<vector<>>' 类型转换为非标量类型
- 预处理器:插入结构名称中的前一个行号
- 保持排序的数据结构,允许log N插入时间,并且可以返回我在log N中查找的元素的索引
- 为什么查找 + 插入的组合比单个插入语句工作得更快
- 查找并插入-STD :: MAP C
- 是否有具有对数时间插入、删除和查找(带距离)的排序数据结构
- 在二叉树中插入/查找问题
- 插入加密的 USB 驱动器后,如何使用 WMI 查找"启动器"逻辑磁盘?
- 具有非常快速的内>数据查找和快速反向查找(搜索/插入/删除数据)的压缩字典?
- C++ std::lower_bound() 函数来查找索引排序向量的插入点
- 是否有 std 或提升容器可以避免其插入和查找方法之间的争用条件
- 插入号码的常量时间查找
- 使用 STL 容器在 logN 中插入、查找最小(键)和删除(基于值)功能
- 对在 std::map 的查找/插入上使用可升级锁感到困惑
- Unordered_map::find()插入查找的键
- "std::map with mutexes"与并行插入、查找、擦除之间是否有任何速度测试"libcds maps (Michael Hashmap and Split Order List)"?
- TBB 并发哈希映射查找和插入
- 用于快速插入和查找n维实向量的适当容器(提供初始基准)