访问没有字符的字符串的第一个字符
Accessing the first Character of a String with no Characters
我在C++中实现了后缀trie。Trie
构造器的实现如下所示。
#include <iostream>
#include <cstring>
#include "Trie.hpp"
using namespace std;
Trie::Trie(string T){
T += "#"; //terminating character
this->T = T;
nodes.reserve(T.length() * (T.length() + 1) / 2); //The number of nodes is bounded above by n(n+1)/2. The reserve prevents reallocation (http://stackoverflow.com/questions/41557421/vectors-and-pointers/41557463)
vector<string> suffix; //vector 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 vector
}
//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
}
}
我正在使用两种数据结构,Node
和 Edge
,它们有一些您期望的 getter 和 setter 以及属性。该方法childLoc()
返回表示给定字符的边(如果存在)的位置。
代码编译得很好,但由于某种原因,我在运行时收到此错误:
terminate called after throwing an instance of 'std::out_of_range'
what(): basic_string::at: __n (which is 0) >= this->size() (which is 0)
Aborted (core dumped)
有人告诉我,此错误意味着我正在访问空字符串的第一个字符,但我看不到代码中发生这种情况的位置。
我看到两个可能负责std::out_of_range
的代码部分:
首先:以下表达式可能会访问位置 0
处的空字符串。这可能会发生在(如第二部分所示)收缩包含在 suffix
-vector 中的字符串时:
int edgeIndex = currentNode->childLoc(suffix[0].at(0));
其次,你对 -vector suffix
中的条目进行操作,风险是字符串会缩短:
suffix[0] = suffix[0].substr(1, suffix[0].length());
std::out_of_range
substr
如果第一个操作数(即 pos
-参数) 超过数组长度(参见字符串::substr):
pos
:要复制为子字符串的第一个字符的位置。如果 这等于字符串长度,函数返回一个空 字符串。如果这大于字符串长度,则抛出 out_of_range。注意:第一个字符用值 0 表示 (不是 1)。
要找出这些表达式中的哪一个实际上负责异常,我建议咨询您的调试器:-)
相关文章:
- 为什么它只打印双链接列表的第一个值,而我的程序却崩溃了
- std::find,返回所有找到的值的替代方法,而不仅仅是存在重复的向量的第一个值
- 如何仅读取文本文件中的第一个值
- 在C++中,如何在第一个"system()"结束后执行第二个"system()"?
- 查找不在标准中的第一个值::设置<int>最小-最大值
- C++:忽略第一个 cin.ignore 之后的输入
- C++:使用另一个字符将一个字符大写
- 我可以得到一个字符 * 到一个 std::sregex_iterator 匹配 str() 吗?
- 在C++中打印多个矢量的第一个值
- C++去除前x个元素的有效方法,在不改变向量大小的情况下将第x+1个元素推到第一个
- C++第一个cout将不会打印
- 我们可以在第一个else-if条件结束后使用另一个else-if条件吗
- OpenGL:第二个VBO破坏了第一个VBO
- 将字符从一个字符串简单分配到另一个字符串不起作用
- 为什么第一个Dynamic_cast没有投射到基类?
- 将第一个n个字符复制到std :: string
- 一个奇怪的字符出现在我的字符数组的第一个
- 当一长串字符传递给第一个 cin 时,为什么不暂停第二个 cin 上的输入?
- 哪个字符是 c++ 中 4 个字符中的第一个
- 从 std::string 中提取(第一个)UTF-8 字符