使用具有结构数据类型的无序多重映射



我需要定义一个无序的多映射,它包含一个int类型的键和一个"类型的值;点头";。

Nod.h

#pragma once
#include <iostream>
struct nod {
int stare[100], pathManhattan, depth;
nod* nodParinte;
char actiune;
nod();
void Citire(int n);
void Init();
};

节点.cpp

#include "Nod.h"
#include "ConsoleApplication1.cpp"
nod::nod()
{
pathManhattan = 0;
depth = 0;
nodParinte = NULL;
}
void nod::Citire(int n)
{
for (int i = 0; i < n*n; i++)
{
std::cin >> stare[i];
}
}
void nod::Init()
{
theExplored.insert({ pathManhattan + depth, this });
}

问题出在Init函数上。我不知道该怎么做。控制台应用程序1.cpp

#include <iostream>
#include <string>
#include <unordered_map>
#include "Nod.h"
std::unordered_multimap<int, nod> theExplored;
std::unordered_multimap<int, nod> theFrontier;
int main()
{
return 0;
}

在我看来,它应该可以工作,因为每个nod都会有一个init函数,它会在Explored中插入它所使用的节点,但我认为我可能没有正确理解unordered_multimap。

我的逻辑有缺陷吗?如果是这样的话,我应该如何创建一个键为int、值为nod类型的哈希表(AFAIK unorderede_multimap基本上是一个哈希表(?

此外,我如何在结构中生成一个函数,以便将节点插入哈希表?

谢谢。

我认为您需要为应用程序源ConsoleApplication1.cpp中的映射定义创建一个头文件。您不能只#包含源代码,因为这将为您的映射提供多个定义。

你可以这样做,创建ConsoleApplication1.h:

#pragma once
#include <unordered_map>
#include "Nod.h"
extern std::unordered_multimap<int, nod> theExplored;
extern std::unordered_multimap<int, nod> theFrontier;

请注意extern关键字。这意味着代码只是一个声明,即实际的映射是在程序的其他地方(在外部文件中(定义的。

当然,您的定义ConsoleApplication1.cpp中。

现在您可以将行#include "ConsoleApplication1.cpp"更改为#include "ConsoleApplication1.h",这将解决您的结构问题。

最新更新