c++ - 如何在 C++ 中使用空值初始化 char 到 char 的映射?
问题描述
我正在尝试将每个节点的父节点存储在无序映射中,并且需要使用 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。PS:我是 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;
}
解决方案
C++ 没有“空值”的概念。所有整数都有整数值,所有字符都有 char 值,所有字符串都有字符串值,所有指针都有指针值。总是。现在,您可能有一个将其视为空的值,例如nullptr
指针、'\0'
字符和0
整数,但变量仍然存在,将其作为值保存。
您通常可以只使用{}
来获取任何类型的默认值,如果您愿意,可以将其视为神奇的“无价值”。或者,您可以使用,除了 的任何有效值之外std::optional<T>
,它还可以具有 的值。std::nullopt
T
推荐阅读
- node.js - 使用 NodeJS 的 readline 模块时如何正确处理错误
- delphi - Is there a way to get just the ANSI characters from a string? Utf8decode fails when string contains emojis
- go - 有没有办法在本地机器上拥有所需的模块而不是让它们联机
- powershell - Powershell Copy-Item -recurse 不拾取新目录
- amazon-web-services - 如何使用跳转主机在 jenkins 上配置 ssh 服务器?
- javascript - VUE JS - 我的一些方法在没有我调用的情况下被调用
- css - 在 React 中更改不允许的拖动光标
- c# - 如何在 WPF 中通过 XAML 岛使用 Windows 10 样式资源
- string - 将 > 作为变量,因此它可以是 < 或 > 在 Case 是
- python - Torchvision 中的转换不起作用