C++ 二叉搜索树的运算符重载
我相信我的运算符重载对于我的两种数据类型之一不起作用。我的程序使用int数据类型,但不使用我定义的类学生。学生是一棵指针树,所以这可能是我的问题。当我的搜索功能没有按计划运行时,我发现了这个问题。我希望能得到一些建议,看看我的重载操作符到底出了什么问题C++ 二叉搜索树的运算符重载,c++,templates,operator-overloading,binary-search-tree,C++,Templates,Operator Overloading,Binary Search Tree,我相信我的运算符重载对于我的两种数据类型之一不起作用。我的程序使用int数据类型,但不使用我定义的类学生。学生是一棵指针树,所以这可能是我的问题。当我的搜索功能没有按计划运行时,我发现了这个问题。我希望能得到一些建议,看看我的重载操作符到底出了什么问题 struct Students{ char lastName[20]; char firstName[20]; int IDnumber; Students(); bool operator == (const Students); bool
struct Students{
char lastName[20];
char firstName[20];
int IDnumber;
Students();
bool operator == (const Students);
bool operator > (const Students);
bool operator < (const Students);
friend ostream& operator << (ostream& stream, const Students* students);
};
template <class DataType>
TNode<DataType>* BST<DataType>::bstSearch(DataType search)
{
TNode<DataType>* y = root;
while (y!=NULL && search != y->data)
{
if (search < y->data)
{
y = y->left;
}
else
{
y = y->right;
}
}
return y;
}
struct学生{
char lastName[20];
charfirstname[20];
国际电话号码;
学生();
布尔运算符==(常量学生);
布尔运算符>(常量学生);
bool运算符<(常量学生);
friend ostream和操作员数据)
{
如果(搜索数据)
{
y=y->左;
}
其他的
{
y=y->右;
}
}
返回y;
}
这是我的重载代码
friend bool operator == (const Students& lh, const Students& rh);
friend bool operator > (const Students& lh, const Students& rh);
friend bool operator < (const Students& lh, const Students& rh);
bool operator ==(const Students& lh, const Students& rh)
{
return lh.IDnumber == rh.IDnumber;
}
bool operator > (const Students& lh, const Students& rh)
{
return lh.IDnumber > rh.IDnumber;
}
bool operator < (const Students& lh, const Students& rh)
{
return lh.IDnumber < rh.IDnumber;
}
friend bool操作符==(常量学生和左侧,常量学生和右侧);
friend bool操作符>(常量学生和左侧,常量学生和右侧);
friend bool运算符<(常量学生和左侧,常量学生和右侧);
布尔运算符==(常量学生和左侧、常量学生和右侧)
{
返回lh.IDnumber==rh.IDnumber;
}
布尔运算符>(常数学生和左侧,常数学生和右侧)
{
返回左IDnumber>右IDnumber;
}
bool操作员<(连续学生和左侧,连续学生和右侧)
{
返回左IDnumber<右IDnumber;
}
这是我创建的树对象
BST<Students*> stree;
BST<int> itree;
BST街;
英国理工学院;
如果您使用学生*
作为数据类型,那么:
if (search < y->data)
另外,不要将对象按值传递给运算符和搜索函数:
bool Students::operator< (const Students& ob)
TNode<DataType>* BST<DataType>::bstSearch(const DataType& search)
因此,只需添加dereference
模板,在search
函数中,您可以将其用作:
template <class DataType>
TNode<DataType>* BST<DataType>::bstSearch(DataType search)
{
TNode<DataType>* y = root;
while (y!=NULL && dereference(search) != dereference(y->data))
{
if (dereference(search) < dereference(y->data))
{
y = y->left;
}
else
{
y = y->right;
}
}
return y;
}
模板
TNode*BST::bstSearch(数据类型搜索)
{
TNode*y=根;
while(y!=NULL&&dereference(搜索)!=dereference(y->data))
{
if(取消引用(搜索)<取消引用(y->数据))
{
y=y->左;
}
其他的
{
y=y->右;
}
}
返回y;
}
有关解引用方法的更多信息,请参见此处对我的问题的优秀答案:如果您想将其保留为指针树,则需要进行如下比较:
if (*search < *(y->data)) {
if(*搜索<*(y->数据)){
在我看来,int
是非指针类型,Students*
是指针类型。如果您在树中使用
Students
而不是Students*
,它应该可以工作。或者,您可以使用不同的实现来处理指针树。我对运算符重载做了一些改进。有没有办法更改我的重载代码来修复此问题,而不将BST更改为传递非指针?例如,如果我将运算符更改为接受bool Students::operator<(const Students*ob)这行得通吗?@user2105982查看我的编辑以获得一些想法。此代码使用C++11。但是对于
BST
,这将失败。啊,我想我的问题在于知道TNode
是什么。
#include <utility>
#include <iostream>
template<typename T>
T& dereference(T &v){return v;}
template<typename T>
const T& dereference(const T& v){return v;}
template<typename T>
typename std::enable_if<!std::is_pointer<T>::value, T&>::type dereference(T* v){return dereference(*v);}
// example usage:
template <typename T>
class A
{
public:
bool compare(T a, T b){
return dereference(a) < dereference(b);
}
};
int main()
{
int u = 10;
int *v = &u;
int i = 5;
int *j = &i;
A<int> a;
A<int*> b;
std::cout << a.compare(i, u) << std::endl;
std::cout << b.compare(j, v) << std::endl;
return 0;
}
template <class DataType>
TNode<DataType>* BST<DataType>::bstSearch(DataType search)
{
TNode<DataType>* y = root;
while (y!=NULL && dereference(search) != dereference(y->data))
{
if (dereference(search) < dereference(y->data))
{
y = y->left;
}
else
{
y = y->right;
}
}
return y;
}
if (*search < *(y->data)) {