二叉搜索树重载==操作符或只需要一般建议
binary search tree overloading == operator or just need general advice
我正在编写一个程序,该程序读取用户输入的文本文件,创建用户输入的文本文件,为用户想要的文本文件命名,然后对文本文件进行排序,对用户输入阈值以上的单词进行排序,并将单词及其被找到的次数显示到用户指定的输出文件中。我已经完成了大部分代码,但我得到了一个编译器错误在这里的样本输出,错误和代码
的示例输出Enter name of input command file; press return.
history.in
Enter name of output file; press return.
history.out
Enter name of test run; press return.
sample
Enter the minimum size word to be considered.
5
Sample results (found in user specified output file):
sample
abacus 4
abstract 1
adding 1
addition 2
advances 1
after 3
,其中单词是在文本文件中找到的单词,它旁边的数字是它被找到的次数。
错误代码C:Userskevin jackDesktopprog-4>g++ -o try main.cpp
main.cpp: In function `void Process(TreeNode*&, StrType)':
main.cpp:49: error: no match for 'operator==' in 'tree->TreeNode::info.WordType:
:word == s'
main.cpp:51: error: no match for 'operator<' in 's < tree->TreeNode::info.WordTy
pe::word'
main.cpp
//main.cpp
#include <fstream>
#include "StrType.h"
#include <cstddef>
#include <iostream>
#include <string>
using namespace std;
struct WordType
{
public:
StrType word;
int count;
};
struct TreeNode
{
WordType info;
TreeNode* left;
TreeNode* right;
};
class ListType
{
public:
ListType();
void InsertOrIncrement (StrType string);
void Print(std::ofstream&) const;
private:
TreeNode* root;
};
ListType::ListType()
{
root=NULL;
}
void Process(TreeNode*& tree, StrType s)
{
if(tree == NULL)
{
tree = new TreeNode;
tree->info.word = s;
tree->info.count = 1;
tree->left = NULL;
tree->right = NULL;
}
else if (tree->info.word == s)
tree->info.count++;
else if (s < tree->info.word)
Process(tree->left, s);
else
Process(tree->right, s);
}
void ListType::InsertOrIncrement(StrType s)
{
Process(root, s);
}
void Print (TreeNode* tree, std::ofstream& outFile)
{
if (tree!= NULL)
{
Print(tree->left, outFile);
tree->info.word.PrintToFile(true, outFile);
outFile <<" "<< tree->info.count;
Print(tree->right, outFile);
}
}
void ListType::Print(std::ofstream& outFile) const
{
::Print(root, outFile);
}
int main()
{
using namespace std;
ListType list;
string inFileName;
string outFileName;
string outputLabel;
ifstream inFile;
ofstream outFile;
StrType string;
int minimumLenght;
cout<<"enter in imput file name."<<endl;
cin>>inFileName;
inFile.open(inFileName.c_str());
cout<<"enter name of output file."<<endl;
cin>>outFileName;
outFile.open(outFileName.c_str());
cout<<"enter name of test run."<<endl;
cin>>outputLabel;
outFile<< outputLabel << endl;
cout<<"enter the min word size."<<endl;
cin>>minimumLenght;
string.GetStringFile(true, ALPHA_NUM, inFile);
while(inFile)
{
if(string.LenghtIs() >= minimumLenght)
list.InsertOrIncrement(string);
string.GetStringFile(true, ALPHA_NUM, inFile);
}
list.Print(outFile);
outFile.close();
inFile.close();
return 0;
}
StrType.h
//StrType.h
#include <fstream>
#include <iostream>
const int MAX_CHARS=100;
enum InType{ALPHA_NUM, ALPHA, NON_WHITE, NOT_NEW};
class StrType
{
public:
void MakeEmpty();
void GetString(bool skip, InType charsAllowed);
void GetStringFile(bool skip, InType charsAllowed,
std::ifstream& inFile);
void PrintToScreen(bool newLine);
void PrintToFile(bool newLine, std::ofstream& outFile);
int LenghtIs();
void CopyString(StrType& newString);
private:
char letters[MAX_CHARS + 1];
};
void StrType::MakeEmpty()
{
letters[0] =' ';
}
我认为这是关于重载==操作符,但我不是很擅长重载。如果有人能帮我一把,那就太好了。
您不能重载,而是为您的class StrType
定义 operator==
和operator<
你可以这样做:
class StrType
{
public:
...
bool operator==(const StrType& other) const;
bool operator<(const StrType& other) const;
...
};
bool StrType::operator==(const StrType& other) const
{
return (strcmp(letters, other.letters) == 0);
}
bool StrType::operator<(const StrType& other) const
{
return (strcmp(letters, other.letters) < 0);
}
您需要为struct WordType实现"operator =="answers"operator <"
可以是如下的成员函数:
struct WordType
{
bool operator == (const WordType &other)
{
// implementation
}
bool operator < (const WordType &other)
{
// implementation
}
};
或者像这样的全局函数:
bool operator == (const WordType &left, const WordType &right)
{
// implementation
}
bool operator < (const WordType &left, const WordType &right)
{
// implementation
}
相关文章:
- 有根的二进制搜索树.保留与其父级的链接
- 正在尝试重载二进制搜索树分配运算符
- 使用C++创建特殊的二叉搜索树
- 在递归二叉搜索树中搜索
- 在二叉搜索树中插入时出现分段错误
- C++二叉搜索树模板从函数返回节点
- 我可以在没有堆栈的情况下在二叉搜索树中实现迭代器吗?
- 在二叉搜索树C++中计算平均值
- 在字符串的二叉搜索树中搜索子字符串 - C++
- 在二叉搜索树中插入新元素
- 如何打印在二叉搜索树中找到的数据?
- 查找存储在二叉搜索树的所有非叶子中的数据总和?(返回整数的独立递归函数
- 这是一个二叉搜索树吗?黑客排名问题
- 二叉搜索树 - 实现"search"函数
- 二叉搜索树的 C++ 实现中的EXC_BAD_ACCESS错误
- 删除二叉搜索树 (C++) 中的单个元素
- 将值存储在带有C++的二叉搜索树中
- 二叉搜索树<<运算符重载不起作用
- 使用二进制搜索树重载运算符
- 二叉搜索树重载==操作符或只需要一般建议