地图和集合,请说明为什么此代码具有输出

发布于 2025-01-21 13:16:43 字数 616 浏览 3 评论 0原文

地图和集合 请解释为什么此代码具有输出 我对m [x] ++和排名3:0的尤其感到困惑 我的理解是该集合是(0,1,2,2,4,4)和m。插入(密钥,值)将记录地图(0,2,4,4,4,8,8),这将使输出0:0 1:2 2:4 3:4 4:8请告诉我为什么这是不正确的。

set < int > s ;
map < int , int > m ;
s.insert (0);
s.insert (4);
s.insert (2);
s.insert (4);
s.insert (1);
s.insert (2);
for ( int x : s ) {
    if ( m.count ( x ) == 0) {
        m.insert ({ x , x * 2});
    } else {
        m [ x ]++;
    }
}
for ( int i = 0; i < 5; i ++) {
    cout << i << ": " << m [ i ] << endl ;
}  

实际输出:

0:0
1:2
2:4
3:0 4:8

Maps and sets
please explain why this code has the output
I am especially confused about what m[x] ++ and the out put 3:0
my understanding was that the set is (0,1,2,2,4,4) and m. insert(key,value) would record the map (0,2,4,4,8,8) which would make the output 0:0 1:2 2:4 3:4 4:8 please tell my why this is incorrect.

set < int > s ;
map < int , int > m ;
s.insert (0);
s.insert (4);
s.insert (2);
s.insert (4);
s.insert (1);
s.insert (2);
for ( int x : s ) {
    if ( m.count ( x ) == 0) {
        m.insert ({ x , x * 2});
    } else {
        m [ x ]++;
    }
}
for ( int i = 0; i < 5; i ++) {
    cout << i << ": " << m [ i ] << endl ;
}  

actual output:

0: 0
1: 2
2: 4
3: 0
4: 8

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

野却迷人 2025-01-28 13:16:43

让我们以多个部分打破您的代码。

set < int > s ;
map < int , int > m ;
s.insert (0);
s.insert (4);
s.insert (2);
s.insert (4);
s.insert (1);
s.insert (2);

首先,您将填充一组带有整数。由于std ::集可以保证单一和订购,因此您的集合包含值{0、1、2、4}


for ( int x : s ) {
    if ( m.count ( x ) == 0) {
        m.insert ({ x , x * 2});
    } else {
        m [ x ]++;
    }
}

现在,对于设置上的每个唯一值,您正在查看地图上是否存在此值作为密钥的条目。由于您的地图起初是空的,因此您将始终通过此测试,并且永远不会进行其他部分。

因此,您的地图包含{[0:0],[1:2],[2:4],[4:8]}

如果您使用过向量而不是集合,则某些值本可以重复的,而您的地图将是:{[0:0],[1:2],[2:5],[4:9]}


for ( int i = 0; i < 5; i ++) {
    cout << i << ": " << m [ i ] << endl ;
}  

最后,您打印了内容在终端上的地图。等待,您的地图不包含键3的值!
通过使用操作员 [] https:// en.cppreference.com/w/cpp/container/map/operator_at ),您已经在地图中插入了一个值,并以默认值0值以方式。
您应该使用方法 at() https ://en.cppreference.com/w/cpp/container/map/at )为了检查该地图中是否存在元素。

Let's break your code in multiple parts.

set < int > s ;
map < int , int > m ;
s.insert (0);
s.insert (4);
s.insert (2);
s.insert (4);
s.insert (1);
s.insert (2);

First, you populate a set with integer. Since a std::set guarantees unicity and ordering, your set contains the values {0, 1, 2, 4}.


for ( int x : s ) {
    if ( m.count ( x ) == 0) {
        m.insert ({ x , x * 2});
    } else {
        m [ x ]++;
    }
}

Now, for each unique value on your set, you are looking if a entry exists on your map with this value as a key. Since your map is empty at first, you will ALWAYS pass this test and never go on the else part.

Thus, your map contains {[0 : 0], [1 : 2], [2 : 4], [4 : 8]}

If you had used a vector instead of a set, some values would have been duplicated, and your map would be instead : {[0 : 0], [1 : 2], [2 : 5], [4 : 9]}


for ( int i = 0; i < 5; i ++) {
    cout << i << ": " << m [ i ] << endl ;
}  

Finally, you print the contents of your map on a terminal. Wait, your map doesn't contains a value for the key 3 !
By using the operator [] (https://en.cppreference.com/w/cpp/container/map/operator_at), you have inserted a value in the map with the default 0 value in the way.
You should have used the method at() (https://en.cppreference.com/w/cpp/container/map/at) in order to check whether an element exists in this map or not.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文