C++ Trie C++;字符串未完全填充文本文件中的所有值
我试图按字母顺序和长度对大量字符串进行排序,似乎我只对所需数量的1/7进行排序。我试图对100000个单词进行排序,当我对70000个单词进行排序时,我最终得到了8000个单词的排序结果,但似乎无法找到它不起作用的原因。任何帮助都将不胜感激C++ Trie C++;字符串未完全填充文本文件中的所有值,c++,string,sorting,trie,radix-sort,C++,String,Sorting,Trie,Radix Sort,我试图按字母顺序和长度对大量字符串进行排序,似乎我只对所需数量的1/7进行排序。我试图对100000个单词进行排序,当我对70000个单词进行排序时,我最终得到了8000个单词的排序结果,但似乎无法找到它不起作用的原因。任何帮助都将不胜感激 #include<iostream> #include<fstream> #include<string> #include<vector> using namespace std; class Node
#include<iostream>
#include<fstream>
#include<string>
#include<vector>
using namespace std;
class Node
{
public:
char value; // the character value
bool end; // indicates whether this node completes a word
Node * children[93]; // represents the 93 ascii values for 33-126
Node(char newChar);
~Node();
};
class Trie
{
public:
Trie();
~Trie();
void addWord(string word);
Node * getRoot();
private:
Node * root;
};
Node::Node(char newChar)
{
value = newChar;
for (int i = 0; i < 93; ++i)
children[i] = NULL;
}
Node::~Node()
{
delete[] children;
}
Trie::Trie()
{
root = new Node(' ');
root->end = true;
}
Trie::~Trie()
{
delete root;
}
Node * Trie::getRoot()
{
return root;
}
void Trie::addWord(string word)
{
Node * currentNode = root;
for (int i = 0; i < word.size(); ++i)
{
char currentChar = word.at(i);
int index = currentChar - '!';
if (currentNode->children[index] != NULL)
{
currentNode = currentNode->children[index];
}
else
{
Node * newNode = new Node(currentChar);
currentNode->children[index] = newNode;
currentNode = newNode;
}
if (i == word.size() - 1)
{
currentNode->end = true;
}
}
}
void alphabetize(Node * node, vector<string> & sorting, string prefix = "") //why dont i have to declare this?
{
if (node->end)
{
sorting.push_back(prefix);
}
for (int i = 0; i < 93; ++i)
{
if (node->children[i] != NULL)
{
string currentString = prefix + node->children[i]->value; //store all characters
alphabetize(node->children[i], sorting, currentString);
}
else
{
}
}
}
int main()
{
Trie * t = new Trie();
string tempS;
int lengthCounter = 0;
ifstream fin;
fin.open("test.txt");
vector< vector<string> > sortLength;
vector <string> row(0, "");
vector<string> sorted;
while(fin >> tempS)
{
while(tempS.length() > lengthCounter)
{
sortLength.push_back(row);
lengthCounter++;
}
t->addWord(tempS);
}
alphabetize(t->getRoot(),sorted); //filled with sorted vector
for(int i = 0; i < sorted.size(); i++)
{
sortLength[sorted[i].length()-1].push_back(sorted[i]);
}
for(int k = 0; k < sortLength.size(); k++)
{
for(int l = 0; l < sortLength[k].size(); l++)
{
cout << sortLength[k][l] << "\n";
}
}
cout << sorted.size();
return 0;
}
#包括
#包括
#包括
#包括
使用名称空间std;
类节点
{
公众:
char value;//字符值
bool end;//指示此节点是否完成一个字
Node*children[93];//表示33-126的93个ascii值
节点(charnewchar);
~Node();
};
三类
{
公众:
Trie();
~Trie();
void addWord(字符串字);
Node*getRoot();
私人:
节点*根;
};
节点::节点(char newChar)
{
value=newChar;
对于(int i=0;i<93;++i)
children[i]=NULL;
}
节点::~Node()
{
删除[]个子女;
}
Trie::Trie()
{
根=新节点(“”);
root->end=true;
}
特里亚::~特里亚()
{
删除根;
}
节点*Trie::getRoot()
{
返回根;
}
void Trie::addWord(字符串字)
{
节点*当前节点=根;
for(int i=0;ichildren[index]!=NULL)
{
currentNode=currentNode->children[索引];
}
其他的
{
Node*newNode=新节点(currentChar);
currentNode->children[index]=newNode;
currentNode=newNode;
}
if(i==word.size()-1)
{
currentNode->end=true;
}
}
}
void字母顺序(Node*Node,vector&sorting,string prefix=“”)//为什么我不需要声明这个?
{
如果(节点->结束)
{
排序。推回(前缀);
}
对于(int i=0;i<93;++i)
{
如果(节点->子项[i]!=NULL)
{
string currentString=prefix+node->children[i]->value;//存储所有字符
按字母顺序排列(节点->子项[i],排序,当前字符串);
}
其他的
{
}
}
}
int main()
{
Trie*t=新的Trie();
弦乐节拍;
int长度计数器=0;
流鳍;
fin.open(“test.txt”);
向量<向量>排序长度;
向量行(0,“”);
向量排序;
同时(fin>>临时工)
{
while(temp.length()>长度计数器)
{
sortLength.向后推(世界其他地区);
长度计数器++;
}
t->addWord(tempS);
}
按字母顺序排列(t->getRoot(),排序);//用排序的向量填充
对于(int i=0;i coutdelete[]子项;
这并不像您认为的那样,delete[]
将删除分配了new[]的数组
,如果你想删除子元素
指向的元素,你必须在数组上循环并逐个删除每个元素。然后你几乎肯定不应该首先处理原始数组和指针。打开编译器警告。我没想到它会像我想的那样,但是我并不认为这与我的问题有关,这就是为什么在字母顺序中你使用for(int I=0;I<93;++I)
?93
代表什么?应该是基数128,不是吗?我是针对ascii表上的值33-126做的