C++ 将 std::map 数据复制到另一个地图
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5103532/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Copy std::map data to another map
提问by herzl shemuelian
I have a map that's defined like this
我有一个这样定义的地图
struct A
{
int A;
int B;
};
typedef map<int,A> Amap;
Then I have Amap1
and I want copy it to Amap2
然后我有Amap1
,我想把它复制到Amap2
A a....;
Amap Amap1,Amap2;
Amap1[1]=a1;
Amap1[2]=a2;
Amap1[3]=a3;
Amap2.insert(Amap1.begin(), Amap1.end());
Sometimes this work properly, sometimes this copies only the keys and the value 0. Where is my mistake here?
有时这可以正常工作,有时这仅复制键和值 0。我的错误在哪里?
回答by Armen Tsirunyan
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)
回答by templatetypedef
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
键/值对转换为map
已经拥有该键的键,那么旧值将被保留,而新值将被丢弃。出于这个原因,如果你写
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
,请考虑仅使用赋值运算符:
Amap2 = Amap1;
This will blindly discard the contents of Amap2
, though, so be careful when doing this.
Amap2
但是,这会盲目地丢弃 的内容,因此在执行此操作时要小心。
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:
如果你想要做的是添加所有键/值对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());
}
With this, you can write
有了这个,你可以写
MergeMaps(Amap1, Amap2);
To copy all the key/value pairs from Amap2
into Amap1
.
将所有键/值对从复制Amap2
到Amap1
.
Hope this helps!
希望这可以帮助!