简体   繁体   English

C++ 更新unordered_map中的值,其中键值对的数据类型为int-unordered_set

[英]C++ Updating values in a unordered_map, where the data type of key-value pair is int-unordered_set

I was solving a DSA question and observed a slightly strange behavior in the C++ standard template library:我正在解决一个 DSA 问题并在 C++ 标准模板库中观察到一个稍微奇怪的行为:

    vector<int> v = {1, 7, 8, 3, 12};

    unordered_map<int, unordered_set<int>> ump;

    for (int i=0; i<v.size(); i++) {
        unordered_set<int> us;
        us.insert(56);
        ump.insert(mp(v[i], us));
    }
    
    for (int i=0; i<v.size(); i++) {
        unordered_set<int> us = ump.find(v[i])->second;
        us.insert(67);
    }

    for (auto it = ump.begin(); it != ump.end(); it++) {
        cout << it->first << ": ";
        for (auto ait = it->second.begin(); ait!=it->second.end(); ait++) {
            cout << *ait << ' ';
        }
        cout << '\n';
    }

Here the output is coming to be:这里的 output 将成为:

12: 56 
3: 56 
8: 56 
7: 56 
1: 56 

But the expected output should be:但预期的 output 应该是:

12: 67 56 
3: 67 56 
8: 67 56 
7: 67 56 
1: 67 56

And I guess the problem is arising during the insertion of values into the set.我想问题是在将值插入集合的过程中出现的。 And I also think, the problem is in someway related to the "changing values through their reference".而且我还认为,问题在某种程度上与“通过参考改变价值观”有关。 But not exactly sure what is causing this.但不确定是什么原因造成的。 I experimented a bit more and found that the below code snippet works.我做了更多实验,发现下面的代码片段有效。 But still not sure, what is going wrong in the above code?但是还是不确定,上面的代码哪里出了问题?

In the below code snippet, instead of directly updating the set through its reference, I made a copy of it.在下面的代码片段中,我没有通过引用直接更新集合,而是复制了一份。 Then inserted my values and again copied that into the map, and it worked.然后插入我的值并再次将其复制到 map 中,它起作用了。

    vector<int> v = {1, 7, 8, 3, 12};

    unordered_map<int, unordered_set<int>> ump;

    for (int i=0; i<v.size(); i++) {
        unordered_set<int> us;
        us.insert(56);
        ump.insert(mp(v[i], us));
    }
    
    for (int i=0; i<v.size(); i++) {
        unordered_set<int> us = ump.find(v[i])->second;
        unordered_set<int> us1;
        for (auto it = us.begin(); it!=us.end(); it++) {
            us1.insert(*it);
        }
        us1.insert(67);
        ump.find(v[i])->second = us1;
    }

    for (auto it = ump.begin(); it != ump.end(); it++) {
        cout << it->first << ": ";
        for (auto ait = it->second.begin(); ait!=it->second.end(); ait++) {
            cout << *ait << ' ';
        }
        cout << '\n';
    }

Here the output is exactly equal to the expected one:这里的 output 正好等于预期的值:

12: 67 56 
3: 67 56 
8: 67 56 
7: 67 56 
1: 67 56 
unordered_set<int> us = ump.find(v[i])->second;

Makes a copy of the set so your modifications don't change the value in ump .制作该集合的副本,以便您的修改不会更改ump中的值。 You need to use a reference instead:您需要改用参考:

unordered_set<int>& us = ump.find(v[i])->second;

Or using auto :或者使用auto

auto& us = ump.find(v[i])->second;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM