【问题标题】:Copy std::map data to another map将 std::map 数据复制到另一个地图
【发布时间】:2011-02-24 10:46:01
【问题描述】:

我有一张这样定义的地图

 struct A
 {
  int A;
  int B;
 };
 typedef map<int,A> Amap;

然后我有Amap1,我想把它复制到Amap2

 A a....;
 Amap Amap1,Amap2;
 Amap1[1]=a1;
 Amap1[2]=a2;
 Amap1[3]=a3;
 Amap2.insert(Amap1.begin(), Amap1.end());

有时这可以正常工作,有时它只复制键和值 0。我的错误在哪里?

【问题讨论】:

    标签: c++ dictionary stl


    【解决方案1】:

    可以使用 operator = 或复制构造函数将一张地图复制到另一张地图。

    例如

    map<X, Y> mp1; 
    //fill mp1 with data
    map<X, Y> mp2(mp1); //mp2 is a copy of mp1 (via copy-construction)
    map<X, Y> mp3;
    mp3 = mp2; // mp3 is also a copy of mp2 (via copy-assignment)
    

    【讨论】:

    • @Wolf:这两种方法都在答案中提出。这是一个协作编辑的社区,如果您想添加答案,请随时编辑。
    • @Wolf:请不要大幅修改其他人的答案,尤其是五年前写的那些已经被人们支持并被 OP 接受的答案。随意编写您自己的竞争性答案。
    • @LightnessRacesinOrbit 我要求它并且 OP 明确授予我:feel free to edit it
    • @Wolf:OP 错了。我们不会大幅修改答案,除非它们是新的。这对先前的选民或问题作者是不公平的。您可以在 meta 各处找到此指南。谢谢:)
    • @Wolf:这是一个有争议的问题。在 Meta 上被问了很多,我不相信有任何共识。我个人认为,如果要进行实质性更改,作者应该发布一个全新的答案;使所有现有选票无效似乎是不诚实的,无论其意图如何。毕竟,他们被要求对原始内容/建议进行同行评审。
    【解决方案2】:

    假设Amap2 为空,您在上面发布的代码将正常工作。如果您尝试将一个键/值对insert 转换为已经拥有该键的map,那么旧值将被保留而新值将被丢弃。因此,如果你写

    Amap2.insert(Amap1.begin(), Amap1.end());
    

    在某些情况下,您可能无法按预期复制所有内容,因为不会复制重复的键。

    要将Amap2 设置为等于Amap1,请考虑仅使用赋值运算符:

    Amap2 = Amap1;
    

    不过,这会盲目地丢弃Amap2 的内容,所以这样做时要小心。

    如果您想要以完全覆盖现有键/值对的方式将 Amap2 中的所有键/值对添加到 Amap1 中,则可以使用以下逻辑执行此操作。这里的想法类似于合并排序背后的逻辑——我们将映射视为排序值的序列,然后不断地将两者混合在一起:

    void MergeMaps(map<int, A>& lhs, const map<int, A>& rhs) {
        map<int, A>::iterator lhsItr = lhs.begin();
        map<int, A>::const_iterator rhsItr = rhs.begin();
    
        while (lhsItr != lhs.end() && rhsItr != rhs.end()) {
            /* If the rhs value is less than the lhs value, then insert it into the 
               lhs map and skip past it. */
            if (rhsItr->first < lhsItr->first) {
                lhs.insert(lhsItr, *rhsItr); // Use lhsItr as a hint.
                ++rhsItr;
            }
            /* Otherwise, if the values are equal, overwrite the lhs value and move both
               iterators forward. */
            else if (rhsItr->first == lhsItr->first) {
                lhsItr->second = rhsItr->second;
                ++lhsItr; ++rhsItr;
            }
            /* Otherwise the rhs value is bigger, so skip past the lhs value. */
            else
                ++lhsItr;
    
        }
    
        /* At this point we've exhausted one of the two ranges.  Add what's left of the
           rhs values to the lhs map, since we know there are no duplicates there. */
        lhs.insert(rhsItr, rhs.end());
    }
    

    有了这个,你可以写

    MergeMaps(Amap1, Amap2);
    

    将所有键/值对从Amap2 复制到Amap1。

    希望这会有所帮助!

    【讨论】:

    • 如果我想确保现有密钥被覆盖,我可能会选择 2 个选项:我会使用 std::copy 但不是 std::inserter 我会编写一个使用 operator[] 的自定义选项.替代的 2-liner 虽然可能效率不高,但是复制 rhsItr 然后常规插入 lhsItr 到副本中,然后交换。
    • @CashCow:与插入的自定义方法相比,我的方法的主要优点是括号在平均情况下运行 O(lg n) 而我发布的代码应该在线性时间内运行,假设一个好的插入的实现,因为有附近的迭代器的提示。
    猜你喜欢
    • 1970-01-01
    • 2015-09-05
    • 2011-02-14
    • 1970-01-01
    • 1970-01-01
    • 2023-03-15
    • 2012-06-10
    • 2014-03-20
    • 1970-01-01
    相关资源
    最近更新 更多