C++ 如何重新组合地图并将元素插入新地图?

C++ 如何重新组合地图并将元素插入新地图?,c++,stl,map,vector,stdmap,C++,Stl,Map,Vector,Stdmap,我有一张包含以下数据的地图: id prev abundance thing 1573 -1 0 book 1574 1573 39 beds 1575 1574 41 tray 1576 1575 46 cups 我正在编写的代码包含以下内容: struct Abund { int prev; int abundance; string thing; } map<int id, Abund*>

我有一张包含以下数据的地图:

id    prev abundance  thing
1573  -1      0       book
1574  1573    39      beds
1575  1574    41      tray
1576  1575    46      cups
我正在编写的代码包含以下内容:

struct Abund
{
int prev;
int abundance;
string thing;
}

map<int id, Abund*> oldMap;
为此,我创建了一个新的映射和新的结构:

struct NewAbund
{
vector<int> prev2;
vector<int> prevAbun;
vector<int> next2;
vector<int> nextAbun;
string thing2;

NewAbund() : 
prev2(0), 
prevAbun(0), 
next2(0), 
nextAbun(0), 
thing2() {}
NewAbund(
vector<int> nodeprev2, 
vector<int> prev_cnt2, 
vector<int> nodenext2, 
vector<int> next_cnt2, 
string node_thing2) :
prev2(nodeprev2), prevAbun(prev_cnt2), next2(nodenext2), nextAbun(next_cnt2), thing2(node_thing2) {}
}

NewAbund(const Abund& old)
{
    thing2 = old.thing;
    prev2.push_back(old.prev);
    prevAbun.push_back(old.abundance);
}

map<int id, NewAbund*> newMap;
struct NewAbund
{
向量prev2;
向量prevAbun;
向量next2;
向量nextAbun;
字符串2;
NewAbund():
prev2(0),
普雷瓦本(0),
next2(0),
下一步(0),
thing2(){}
纽瓦本德(
向量nodeprev2,
向量prev_cnt2,
向量nodenext2,
向量next_cnt2,
字符串节点(2):
prev2(nodeprev2)、prevAbun(prev_cnt2)、next2(nodenext2)、nextab(next_cnt2)、thing2(node_thing2){}
}
纽阿本德(康斯特阿本德和旧)
{
thing2=旧事物;
prev2.推回(旧的。prev);
前推后推(旧的,富足的);
}
地图新地图;

现在我完全不知道如何将元素从一个地图填充到另一个地图(

您希望将元素从旧映射(
Abund
)填充到新映射(
NewAbund
)的元素中。您需要做的第一件事是将
Abund
对象转换为
NewAbund
对象。这是通过添加新构造函数
NewAbund(const Abund&old)

struct NewAbund
{
向量prev2;
向量prevAbun;
向量next2;
向量nextAbun;
字符串2;
纽阿本德(康斯特阿本德和旧){
//使用旧数据创建一个NewAbund
}
};
一旦我们有了一种将旧数据转换为新数据的方法,我们只需将所有内容从旧地图移动到新地图,如下所示

typedef map<int id, Abund*>::iterator iter;
iter it = oldMap.begin();
iter end = oldMap.end();
for(; it != end; ++it) {
    newMap[it->first] = new NewAbund(*(it->second));
}
typedef映射::迭代器iter;
iter it=oldMap.begin();
iter end=oldMap.end();
for(;it!=end;++it){
newMap[it->first]=newnewabund(*(it->second));
}

我添加了一个构造函数,但我不明白你所说的NewAbund(const Abund&old)是什么意思?
NewAbund(const Abund&old)
是一个构造函数,它接受
Abund
struct(你的旧结构)来创建
NewAbund
struct(新结构).谢谢你帮我,但我真的迷路了!我编辑了我的问题,把我写的东西包括进了《新阿本德》(const Abund&old)constructor@sumrania这应该允许您使用我们拥有的代码的后半部分。您可以使用
newMap[it->first]=newnewabund(*(it->second))插入到新映射中
但是我如何填写向量prev,然后使用它->首先?我应该在某处写一个循环吗?编辑为包含NewAbund(const Abund&old),但仍然没有完成它应该做的事情查看您在图表中给出的示例,当id2=1575时,prev2只包含一个值(1574)。它应该包含更多值吗?
struct NewAbund
{
vector<int> prev2;
vector<int> prevAbun;
vector<int> next2;
vector<int> nextAbun;
string thing2;

NewAbund(const Abund& old) {
    //create a NewAbund using the old data
}

};
typedef map<int id, Abund*>::iterator iter;
iter it = oldMap.begin();
iter end = oldMap.end();
for(; it != end; ++it) {
    newMap[it->first] = new NewAbund(*(it->second));
}