【发布时间】:2021-02-09 17:26:35
【问题描述】:
我正在尝试将每个节点的父节点存储在无序映射中,我需要使用 NULL 初始化值,如下所示:
//This is inside a method of a template class
std::unordered_map<T, T> parent;
parent[start] = NULL;
这会引发警告:
warning: converting to non-pointer type 'std::unordered_map<char, char, std::hash<char>, std::equal_to<char>, std::allocator<std::pair<const char, char> > >::mapped_type' {aka 'char'} from NULL [-Wconversion-null]
parent[start] = NULL;
这在 T 是 char 时有效,但不适用于其他类型。
//This is inside a method of a template class
std::unordered_map<T, T> parent;
parent[start] = '\0';
如何使我可以将键的值存储为 NULL。 P.S:我是 C++ 新手。
T curr = end; // Here end is variable passed by user
while(curr != NULL) { // I want to check whether current is NULL
res.push(curr); // res is a stack, and I push the element(value of key)to it
curr = parent[curr];
}
我想检查一个 NULL 值并停止 while 循环。
这是该方法的完整代码:
#include<iostream>
#include<vector>
#include<algorithm>
#include<unordered_map>
#include<list>
#include<queue>
#include <climits>
#include <stack>
#include <string>
using namespace std;
template <typename T>
class TemplateGraph {
private:
int V;
unordered_map<T, list<pair<T, int>>> adjList;
public:
TemplateGraph(int v): V(v) {}
void addEdge(T from, T to,bool isBiDir, int weight) {
adjList[from].push_back(make_pair(to, weight));
if(isBiDir) {
adjList[to].push_back(make_pair(from, weight));
}
}
void getPath(T start, T end) {
unordered_map<T, int> dist;
priority_queue<pair<T, int>, vector<pair<T, int>>, greater<pair<T, int>>> pq;
unordered_map<T, T> parent;
stack<T> res;
// adjList is of type =
// unordered_map<T, list<pair<T, int>>> adjList;
for(auto vtx: adjList) {
T key = vtx.first;
dist[key] = INT_MAX;
}
pq.push(make_pair(start, 0));
dist[start] = 0;
parent[start] = 0;
while(!pq.empty()){
T top = pq.top().first;
pq.pop();
for(auto nbr: adjList[top]){
T node = nbr.first;
int wt = nbr.second;
int newWt = dist[top] + wt;
if(newWt < dist[node]) {
dist[node] = newWt;
pq.push(make_pair(node, dist[node]));
parent[node] = top;
}
}
}
T curr = end;
while(curr != 0) {
res.push(curr);
curr = parent[curr];
}
while(!res.empty()){
T node = res.top();
res.pop();
cout << node << " ";
}
}
}
int main(){
TemplateGraph<char> g2(9);
g2.addEdge('A', 'B', true, 2);
g2.addEdge('A', 'C', true, 5);
g2.addEdge('B', 'D', true, 7);
g2.addEdge('C', 'D', true, 2);
g2.addEdge('C', 'E', true, 3);
g2.addEdge('E', 'F', true, 4);
g2.addEdge('E', 'H', true, 3);
g2.addEdge('F', 'G', true, 1);
g2.addEdge('D', 'F', true, 1);
g2.getPath('A', 'F');
TemplateGraph<int> g(9);
g.addEdge(1, 2, true, 4);
g.addEdge(4, 1, true, 3);
g.addEdge(2, 3, true, 2);
g.addEdge(2, 5, true, 4);
g.addEdge(4, 5, true, 1);
g.addEdge(3, 8, true, 5);
g.addEdge(3, 7, true, 2);
g.addEdge(7, 9, true, 1);
g.getPath(1, 5);
return 0;
}
【问题讨论】:
-
chars 没有空值。但是,它们的值可以为零。你对NULL有什么期望? “如何使我可以将键的值存储为 NULL” - 看起来您将值设置为NULL- 而不是键。 -
您是否只想根据
T插入'\0'、nullptr、0、0.0之类的“默认值”?也许您还想做一些std::optional可以帮助您的事情。更多代码可能有助于澄清。 -
我想将第一个键初始化为 NULL,并在循环中为每个节点添加父节点。我需要 NULL 作为第一个键,以便在通过跟随其父节点遍历节点时进行范围检查。有点像我们在 Dijkstra 算法中所做的
-
我会建议一种不同的策略 - 根本不要尝试在地图中放置 NULL 值。仅添加具有要分配的实际值的键。您可以使用映射的
find()方法来确定映射中是否存在给定的键,并仅在它确实存在时获取它的迭代器。 -
@RemyLebeau 谢谢!!!现在工作正常...
标签: c++ dictionary data-structures