C ++ unordered

编程入门 行业动态 更新时间:2024-10-24 18:26:48
C ++ unordered_map 如何区分空单元格与0(C++ unordered_map how to distinguish null cell from 0)

假设我有一个unordered_map<string, int> H

假设H中没有元素“house”,所以cout<<H["house"]; 会打印0。

但是假设我需要指定H["house"] = 0 。

如何区分不存在的元素和0的元素?

//example of what I want: if(!H["house"]){ cout<<"DOES NOT EXIST YET\n"; H["house"] = 0; } if(H["house"]){ cout<<"IT EXISTS NOW\n"; }

Suppose I have an unordered_map<string, int> H.

Let's say there is no element "house" in H, so cout<<H["house"]; would print 0.

But suppose I need to assign H["house"] = 0.

How would I distinguish an element that doesn't exist from one that is 0?

//example of what I want: if(!H["house"]){ cout<<"DOES NOT EXIST YET\n"; H["house"] = 0; } if(H["house"]){ cout<<"IT EXISTS NOW\n"; }

最满意答案

最短的测试:

if (H.count("house")) { // "house" exists } else { // "house" does not exist in H }

The shortest test:

if (H.count("house")) { // "house" exists } else { // "house" does not exist in H }

更多推荐

本文发布于:2023-08-06 06:42:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1446109.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:unordered

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!