gpt4 book ai didi

C++ 重新定义 operator<() 和 operator!=()

转载 作者:行者123 更新时间:2023-11-28 07:07:13 27 4
gpt4 key购买 nike

我有这个队列的实现:

#include<iostream>
using namespace std;

template <typename T>
struct elem_q
{
T inf;
elem_q<T>* link;
};

template <typename T = int>
class Queue
{
public:
Queue();
~Queue();
Queue(const Queue&);
Queue& operator= (const Queue&);

bool empty()const;
void push(const T&);
void pop(T&);
void head(T&) const;
void print();
int length();

private:
elem_q<T> *front;
elem_q<T> *rear;

void copyQueue(const Queue<T>);
void deleteQueue();
};

template <typename T>
Queue<T>::Queue()
{
front = rear = NULL;
}

template <typename T>
Queue<T>::~Queue()
{
deleteQueue();
}

template <typename T>
Queue<T>::Queue(const Queue<T>& r)
{
copyQueue(r);
}

template <typename T>
Queue<T>& Queue<T>::operator=(const Queue<T>& r)
{
if(this != &r)
{
deleteQueue();
copyQueue(r);
}
return *this;
}

template <typename T>
void Queue<T>::copyQueue(const Queue<T> r)
{
front = rear = NULL;
elem_q<T> *p = r.front;
while(p)
{
push(p->inf);
p = p->link;
}
}

template <typename T>
void Queue<T>::deleteQueue()
{
T x;
while (!empty())
{
pop(x);
}
}

template <typename T>
bool Queue<T>::empty() const
{
return rear == NULL;
}

template <typename T>
void Queue<T>::push(const T& x)
{
elem_q<T> *p = new elem_q<T>;
p->inf = x;
p->link = NULL;
if (rear) rear->link = p;
else front = p;
rear = p;
}

template <typename T>
void Queue<T>::pop(T& x)
{
if(empty())
{
cout<<"The queue is empty.\n";
}
else
{
elem_q<T> *q = front;
x = q->inf;
if (q == rear)
{
rear = NULL;
front = NULL;
}
else front = q->link;
delete q;
}
}

template <typename T>
void Queue<T>::head(T& x) const
{
if(empty())
{
cout<<"The queue is empty.\n";
}
else
{
x = front->inf;
}
}

template <typename T>
void Queue<T>::print()
{
T x;
while(!empty())
{
pop(x);
cout<<x<<" ";
}
cout<<endl;
}

template <typename T>
int Queue<T>::length()
{
T x;
int n = 0;
while(!empty())
{
pop(x);
n++;
}
return n;
}
template<typename T>
void minqueue(Queue<T> q,T& min,Queue<T>& newq)
{
T x;
q.pop(min);
while (!q.empty())
{
q.pop(x);
if (x < min)
{
newq.push(min);
min = x;
}
else newq.push(x);
}

}
template<typename T>
void sortQueue(Queue<T> q,Queue<T>& newq)
{
while(!q.empty())
{
T min;
Queue<T> q1;
minqueue(q , min ,q1);
newq.push(min);
q = q1;
}

}
template<typename T>
Queue<T> merge(Queue<T> p,Queue<T> q,const T& dummy)
{
p.push(dummy);
q.push(dummy);
Queue<T> r;
T x,y;
p.pop(x);
q.pop(y);
while (!p.empty() && !q.empty())
if (x < y)
{
r.push(x);
p.pop(x);
}
else
{
r.push(y);
q.pop(y);
}
if (!p.empty())
do
{
r.push(x);
p.pop(x);
}while (x != dummy);
else
do
{
r.push(y);
q.pop(y);
}while (y != dummy);
return r;
}

我如何重新定义运算符 < 和 != 因为没有它们函数 minqueue、Sortqueue 和 merge 不起作用?请帮助我...................... .....................................

最佳答案

template <typename T>
struct elem_q
{
T inf;
elem_q<T>* link;
};

template <typename T>
bool operator <( const elem_q<T> &lhs, const elem_q<T> &rhs )
{
return ( lhs.inf < rhs.inf );
}

template <typename T>
bool operator ==( const elem_q<T> &lhs, const elem_q<T> &rhs )
{
return ( lhs.inf == rhs.inf );
}

template <typename T>
bool operator !=( const elem_q<T> &lhs, const elem_q<T> &rhs )
{
return ( !( lhs.inf == rhs.inf ) );
}

关于C++ 重新定义 operator<() 和 operator!=(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21614908/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com