使用OOP/C++实现后缀Trie
Implementing a Suffix Trie using OOP/C++
我正试图在C++中为编程赋值实现后缀trie。现在我认为我的想法是正确的,但我一直遇到分割错误,我一直无法找到原因。
对于这项任务,我们鼓励使用VIM/其他一些基本的文本编辑器,并从控制台编译程序。尽管如此,我还是下载了CLion来尝试调试代码,这样我就可以找到错误了。
现在,当在CLion中运行时,我收到消息
terminate called after throwing an instance of 'std::bad_alloc'
what(): std::bad_alloc
尝试运行调试器会显示消息
Error during pretty printers setup:
Undefined info command: "pretty-printer". Try "help info".
Some features and performance optimizations will not be available.
我是CLion的新手,不知道该怎么办(我唯一使用的JetBrains IDE是Pycharm)。你能帮我解决这个问题吗?
现在,程序本身由三个类组成,Trie
、Edge
和Node
,它们的实现如下所示。Trie实现背后的主要思想是Trie.cpp
的构造函数。
下面详细介绍了代码。我感谢你的帮助。
Main.cpp
#include <iostream>
using namespace std;
#include "Trie.hpp"
int main(){
string s = "Stef";
Trie trie(s);
return 0;
}
尝试.hpp
#ifndef TRIE_HPP
#define TRIE_HPP
#include <string>
#include "Node.hpp"
#include "Edge.hpp"
using namespace std;
class Trie{
private:
string T;
vector<Node> nodes;
void addWord(Node*, string);
public:
Trie(string);
};
#endif
尝试.cpp
#include <iostream>
#include <cstring>
#include "Trie.hpp"
using namespace std;
Trie::Trie(string T){
T += "#"; //terminating character
this->T = T;
vector<string> suffix; //array of suffixes
for(unsigned int i = 0; i < T.length(); i++)
suffix.push_back(T.substr(i, T.length()-i));
//Create the Root, and start from it
nodes.push_back(Node("")); //root has blank label
Node* currentNode = &nodes[0];
//While there are words in the array of suffixes
while(!suffix.empty()){
//If the character under consideration already has an edge, then this will be its index. Otherwise, it's -1.
int edgeIndex = currentNode->childLoc(suffix[0].at(0));
//If there is no such edge, add the rest of the word
if(edgeIndex == -1){
addWord(currentNode, suffix[0]); //add rest of word
suffix.erase(suffix.begin()); //erase the suffix from the suffix array
break; //break from the for loop
}
//if there is
else{
currentNode = (currentNode->getEdge(edgeIndex))->getTo(); //current Node is the next Node
suffix[0] = suffix[0].substr(1, suffix[0].length()); //remove first character
}
}
}
//This function adds the rest of a word
void Trie::addWord(Node* parent, string word){
for(unsigned int i = 0; i < word.length(); i++){ //For each remaining letter
nodes.push_back(Node(parent->getLabel()+word.at(i))); //Add a node with label of parent + label of edge
Edge e(word.at(i), parent, &nodes.back()); //Create an edge joining the parent to the node we just added
parent->addEdge(e); //Join the two with this edge
}
}
节点.hpp
#ifndef NODE_HPP
#define NODE_HPP
#include <string>
#include <vector>
#include "Edge.hpp"
using namespace std;
class Node{
private:
string label;
vector<Edge> outgoing_edges;
public:
Node();
Node(string);
string getLabel();
int childLoc(char);
void addEdge(Edge);
Edge* getEdge(int);
};
#endif
节点.cpp
#include "Node.hpp"
using namespace std;
Node::Node(){
}
Node::Node(string label){
this->label = label;
}
string Node::getLabel(){
return label;
}
//This function returns the edge matching the given label, returning -1 if there is no such edge.
int Node::childLoc(char label){
int loc = -1;
for(unsigned int i = 0; i < outgoing_edges.size(); i++)
if(outgoing_edges[i].getLabel() == label)
loc = i;
return loc;
}
void Node::addEdge(Edge e){
outgoing_edges.push_back(e);
}
Edge* Node::getEdge(int n){
return &outgoing_edges[n];
}
边缘.hpp
#ifndef EDGE_HPP
#define EDGE_HPP
#include <string>
using namespace std;
class Node; //Forward definition
class Edge{
private:
char label;
Node* from;
Node* to;
public:
Edge(char, Node*, Node*);
char getLabel();
Node* getTo();
Node* getFrom();
};
#endif
Edge.cpp
#include "Edge.hpp"
using namespace std;
Edge::Edge(char label, Node* from, Node* to){
this->label = label;
this->from = from;
this->to = to;
}
char Edge::getLabel(){
return label;
}
Node* Edge::getFrom(){
return from;
}
Node* Edge::getTo(){
return to;
}
&nodes[0];
,&nodes.back()
-您将指针存储到vector
中以供以后使用,当您向其添加元素时,向量的底层存储被重新定位时,这些指针将无效。
在你最喜欢的C++书中阅读关于指针的一般内容,尤其是动态分配
如果您还没有最喜欢的C++书籍,请从列表中选择一本。
相关文章:
- 增量运算符与后缀混淆
- 这个极客对极客的trie实现是否存在内存泄漏问题
- 在 c++ 中实现 Trie 时出现分段错误
- 如何在OpenSSL库的名称中添加后缀'd'?
- Trie*& 和 Trie** 的意思是 Same?
- Trie 树初始化
- C++17 十六进制浮点文字单精度后缀冲突?
- 使用带有链表的堆栈数据结构将中缀转换为后缀
- 在我的trie实现中出现分段错误
- 高效的字符串截断算法,按顺序删除相等的前缀和后缀
- 使用 trie 数据结构链接不同类型的信息
- 为什么 C++ 程序员更喜欢前缀 ++,而 Java 程序员更喜欢后缀 ++?
- 在qt QFileDialog中设置后缀在linux中不起作用,如何解决?
- C++ 中的 TRIE 数据结构实现
- 用于自定义调试构建的 CMake qt 输入库后缀
- 组合"%"和可选后缀时,自动属性传播有时不起作用
- Trie数据结构的实现
- C++Trie中的浮点异常
- 使用OOP/C++实现后缀Trie
- 在C++中创建trie/后缀树时减少内存使用