简体   繁体   English

如何使用 c++ 中的 null 值将 char 的 map 初始化为 char?

[英]How to initialize a map of char to char with null values in c++?

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:我试图将每个节点的父节点存储在无序的 map 中,我需要使用 NULL 初始化值,如下所示:

//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.这在 T 是 char 但不适用于其他类型时有效。

//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.如何使我可以将键的值存储为 NULL。 PS: I am new to c++. 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];
}

I want to check for a NULL value and stop the while loop.我想检查 NULL 值并停止 while 循环。

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". C++ 没有“空值”的概念。 All integers have integer values, all chars have char values, and all strings have string values, and all pointers have pointer values.所有整数都有 integer 值,所有字符都有 char 值,所有字符串都有字符串值,所有指针都有指针值。 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.现在,您可能有一个将其视为空的值,例如用于指针的nullptr和用于字符'\0'和用于整数的0 ,但变量仍然存在将其作为值保存。

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 .或者,您可以使用std::optional<T> ,除了T的任何有效值之外,它还可以具有std::nullopt的值。

'\0' is of type char. '\0' 是 char 类型。 NULL is a macro of type void * NULL 是 void 类型的宏 *

you can either use '\0' or you can store a char-pointer instead of char.你可以使用'\0'或者你可以存储一个字符指针而不是字符。 for example char*例如字符*

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM