I am trying to store the parent of each node in an unordered map and I need to initialize the values with NULL, like this:
//This is inside a method of a template class
std::unordered_map<T, T> parent;
parent[start] = NULL;
This throws a warning:
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;
This works when T is char but does not work with other types.
//This is inside a method of a template class
std::unordered_map<T, T> parent;
parent[start] = '\0';
How to make it such that I can store the value of the keys as NULL. PS: I am new to 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];
}
I want to check for a NULL value and stop the while loop.
Here is the full code for the method:
#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++ doesn't have the concept of "an empty value". All integers have integer values, all chars have char values, and all strings have string values, and all pointers have pointer values. Always. Now, it could be that you have a value that you treat like empty, such as nullptr
for pointers and '\0'
for chars and 0
for integers, but the variable still exists holds that as a value.
You can usually just use {}
to get the default value for any type, and treat that as a magic "no value" if you wish. Alternatively, you can use std::optional<T>
, which can have the value of std::nullopt
in addition to any valid value of T
.
'\0' is of type char. NULL is a macro of type void *
you can either use '\0' or you can store a char-pointer instead of char. for example char*
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.