-1
我正在尝试创建一个存储电话号码的Trie。在这样做的时候,我会计算存储所有不同数字所需的节点数量。意外的分段错误
下面是代码:
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
class Node{
public:
char number;
vector<Node*> followings;
Node(){
number = ' ';
}
Node(char n){
number = n;
followings.resize(10);
}
};
int main()
{
int N;
cin >> N; cin.ignore();
Node aux(' ');
Node* root = &aux;
Node* node;
int counter = 0;
for (int i = 0; i < N; i++) {
string telephone;
cin >> telephone; cin.ignore();
node = root;
for(int j = 0; j < telephone.size(); j++){
if(node->followings[(int)telephone[j]-48]->number == ' '){
Node aux(telephone[j]);
node->followings[(int)telephone[j]-48] = &aux;
counter++;
}
node = node->followings[(int)telephone[j]-48];
}
}
cout << counter << endl;
}
但是我试图访问一个节点node->followings[(int)telephone[j]-48]->number
数时得到一个分段错误。有谁知道我为什么gettint错误,以及如何解决它?
感谢您提前 我有点新的C++
编译所有警告和调试信息(例如'g ++ -Wall -Wextra -g'如果使用[GCC](http://gcc.gnu.org/)...)然后**使用调试器** (例如'gdb')或许[valgrind](http://valgrind.org/)。你的修复我的代码问题是脱离主题。 –
因为'followings'是nullptrs的一个向量,所以''> number' segfaults。 – freakish
'电话[j] - '0'是便携式的,但'电话[j] -48'不是。 – molbdnilo