如何重新组合地图并将元素插入新地图
How do I regroup a map and insert elements into a new map?
我有一张地图,其中包含以下数据:
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;
我现在需要创建一个新地图,它应该看起来像这样:
id2 prev2 prevAbun next2 nextAbun thing2
1573 1574 39 book
1574 1573 0 1575 41 beds
1575 1574 39 1576 46 tray
1576 1575 41 cups
因此,为此,我创建了一个新映射和新结构:
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;
现在我完全不知道如何将元素从一张地图填充到另一张地图。 :(
你想将旧地图( Abund
) 的元素填充到新地图( NewAbund
的元素中。您需要做的第一件事是将Abund
对象转换为NewAbund
对象。这是通过添加新的构造函数来完成的NewAbund(const Abund& old)
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));
}
相关文章:
- 将二维数组的所有元素插入到一维数组中
- 如何将元素插入和迭代到这种映射.C++
- 无法在构造函数中执行设置元素插入
- 将数组的元素插入映射的时间复杂度是多少?
- 如何将元素插入到标准::地图的共享指针中?
- 如何将 n 个连续元素插入到元素类型不可复制的 std::vector 中?
- 将元素插入链接列表
- 创建一个函数以在给定位置将元素插入到列表中
- 我可以在一行代码中将向量中与条件匹配的所有元素插入到集合中吗?
- 为什么我不能将元素插入数组中的列表?
- 将多个偏移量的多个元素插入到一个矢量中
- 将元素插入排序数组
- 使用算法中的插入函数将元素插入空容器中,未给出预期的结果
- 使用动态分配创建数组并将元素插入其中
- 将元素插入字符串集 c++ 的向量中
- 如何将元素插入独特指针的多维矢量中
- 通过引用同一向量的元素插入到向量中
- 如何将元素插入 BST
- 如何将元素插入矢量的开头
- STL和元素插入中列表的动态分配