我有这样定义的 map

 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。我在这里的错误在哪里?

最佳答案

可以使用operator =或copy构造函数将一个 map 复制到另一个 map 。

例如

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)

08-06 13:52