簡體   English   中英

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

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

我試圖將每個節點的父節點存儲在無序的 map 中,我需要使用 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++ 沒有“空值”的概念。 所有整數都有 integer 值,所有字符都有 char 值,所有字符串都有字符串值,所有指針都有指針值。 總是。 現在,您可能有一個將其視為空的值,例如用於指針的nullptr和用於字符'\0'和用於整數的0 ,但變量仍然存在將其作為值保存。

您通常可以只使用{}來獲取任何類型的默認值,如果您願意,可以將其視為神奇的“無值”。 或者,您可以使用std::optional<T> ,除了T的任何有效值之外,它還可以具有std::nullopt的值。

'\0' 是 char 類型。 NULL 是 void 類型的宏 *

你可以使用'\0'或者你可以存儲一個字符指針而不是字符。 例如字符*

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM