Copy std::map data to another map

前端 未结 2 1000
闹比i
闹比i 2021-02-01 18:48

I have a map that\'s defined like this

 struct A
 {
  int A;
  int B;
 };
 typedef map Amap;

Then I have Amap1 and I

相关标签:
2条回答
  • 2021-02-01 19:10

    Copying one map to another can be done with operator = or the copy constructor.

    E.g

    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)
    
    0 讨论(0)
  • 2021-02-01 19:15

    The code you've posted above will work correctly assuming that Amap2 is empty. If you try to insert a key/value pair into a map that already holds that key, then the old value will be kept and the new one will be discarded. For that reason, if you write

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

    In some circumstances you might not copy everything over as intended, because duplicate keys won't copy.

    To set Amap2 equal to Amap1, consider just using the assignment operator:

    Amap2 = Amap1;
    

    This will blindly discard the contents of Amap2, though, so be careful when doing this.

    If what you want to do is add all the key/value pairs from Amap2 into Amap1 in a way that completely overrides the existing key/value pairs, you can do so using the following logic. The idea here is similar to the logic behind mergesort - we treat the maps as sequences of sorted values and then continuously blend the two together:

    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());
    }
    

    With this, you can write

    MergeMaps(Amap1, Amap2);
    

    To copy all the key/value pairs from Amap2 into Amap1.

    Hope this helps!

    0 讨论(0)
提交回复
热议问题