繁体   English   中英

在C ++中嵌套的unordered_map / hash_map更新

[英]Nested unordered_map / hash_map update in C++

我是C ++的新手,关于unordered_map(或hash_map)存在以下问题:

#include <unordered_map>
#include <iostream>
using namespace std;

int main()
{
    unordered_map<int,int> h1;
    int temp1=0;
    h1.insert(pair<int,int>(0,temp1));
    unordered_map<int, unordered_map<int,int>> h2;
    h2.insert(pair<int, unordered_map<int,int>>(1,h1));
    unordered_map<int, unordered_map<int,int>>::iterator h2_itor=h2.find(1);
    h2_itor->second.find(0)->second++;

    unordered_map<int, unordered_map<int,int>> h3;
    for(int i=0;i<100;i++)
    {
        int first=rand()%10;
        int second=rand()%10;

        unordered_map<int, unordered_map<int,int>>::iterator h3_itor=h3.find(first);
        if(h3_itor!=h3.end())
        {
            unordered_map<int,int> submap=h3_itor->second;
            unordered_map<int,int>::iterator submap_itor=submap.find(second);
            if(submap_itor!=submap.end())
                submap_itor->second++;
            else
                submap.insert(pair<int,int>(second, 1));
        }
        else
        {
            unordered_map<int,int> submap;
            submap.insert(pair<int,int>(second,1));
            h3.insert(pair<int, unordered_map<int,int>>(first,submap));
        }
    }

    return 0;
}

输出是很奇怪的。 对于h1和h2来说似乎可行,这意味着用键0更新了h1中的值(增加了1)。 尽管这看起来很琐碎,但对于h3,我随机插入了一些“对”(第一对,第二对)并使用哈希映射进行计数,但该计数似乎无法更新。 例如,可能是这样的:

insert 1 -> 7 -> 1 
 ... 
now update 1 -> 7 -> 1 to 1 -> 7 -> 2 using my code
fetch: h3.find(1)->second.find(7)->second : it's still 1 but not 2!

这表明更新值不成功。 我知道在Java中这永远不会发生。 那么这个问题在哪里呢?

问题在这里:

unordered_map<int,int> submap = h3_itor->second;

这样会导致整个子图被复制到新的本地submap对象中。 当您离开示波器时销毁它时,您对其所做的所有修改都会丢失。

相反,您可以使用对要修改的实际hashmap元素的引用:

unordered_map<int,int> &submap = h3_itor->second;

这个&应该解决所有问题。

这是代码第二部分的重构版本(我认为)。 我还在生成一致的测试数据集,以便我们可以在每次运行时重现行为(随机性是测试的厌恶之物)。

这是代码。 问题是什么?

#include <unordered_map>
#include <iostream>

using i2i = std::unordered_map<int, int>;
using map_i_to_i2i = std::unordered_map<int, i2i>;

void test_insert(map_i_to_i2i& outer, int v1, int v2)
{
    auto iouter = outer.find(v1);
    if (iouter == outer.end()) {
        std::cout << "case c) [" << v1 << "] = [" << v2 << "] = 1\n";
        outer[v1][v2] = 1;
    }
    else {
        auto& inner = iouter->second;
        auto iinner = inner.find(v2);
        if (iinner == inner.end())
        {
            std::cout << "case b) [" << v1 << "][" << v2 << "] = 1\n";
            inner.emplace_hint(iinner, v2, 1);
        }
        else {
            std::cout << "case c) [" << v1 << "][" << v2 << "] += 1\n";
            iinner->second += 1;
        }
    }
}

int main()
{
    map_i_to_i2i h3;
    for (int passes = 0 ; passes < 3 ; ++passes)
    {
        for (int x = 0 ; x < 2 ; ++x) {
            for (int y = 0 ; y < 2 ; ++y) {
                test_insert(h3, x, y);
            }
        }
    }
    return 0;
}

并不是真正的答案,只是一个供参考:如果这不仅仅是使用迭代器的练习,那么还有一种更简单的方法:

unordered_map<int, unordered_map<int,int>> h3
h3[0][0] = 1;

for (int i=0; i<100; i++ ) {
    int first=rand()%10;
    int second=rand()%10;
    h3[first][second]++;
}

之所以可行,是因为如果缺少一个值, unordered_map::operator[]将默认构造并插入它。 对于地图,这是一个空地图,对于整数,它是零。

如果您想要另一个默认值,则可以使用unordered_map::emplace ,例如:

unordered_map<int, unordered_map<int,int>> h3
h3[0][0] = 2;

for (int i=0; i<100; i++ ) {
    int x=rand()%10;
    int y=rand()%10;
    int& val = h3[x].emplace(y, 1).first->second;
    val *= 2;
}

是的,那有点令人困惑:如果缺少键,则emplace插入指定的值(如果已经存在,则不会覆盖),然后返回std::pair<iterator, bool>

在这里,布尔值会告诉您是否插入了值,并且迭代器本身是std::pair<key,val>*的包装,因此使用.first->second值。

除了更短/更易读之外,这两种方式都更加有效。 在您的代码中,如果不存在该值,您将进行两次查找,但是以上两种方法均只进行一次查找。

暂无
暂无

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

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