cuốn sách gpt4 ai đã làm

C++ định nghĩa lại operator<() và operator!=()

In lại Tác giả: Walker 123 更新时间:2023-11-28 07:07:13 27 4
mua khóa gpt4 Nike

我有这个队列的实现:

#include
sử dụng không gian tên std;

mẫu
struct elem_q
{
T inf;
elem_q* link;
};

template
class Queue
{
công cộng:
Queue();
~Queue();
Queue(const Queue&);
Queue& operator= (const Queue&);

bool empty()const;
void push(const T&);
void pop(T&);
void head(T&) const;
làm mất hiệu lực in();
int length();

riêng tư:
elem_q *front;
elem_q *rear;

void copyQueue(const Queue);
void deleteQueue();
};

mẫu
Queue::Queue()
{
front = rear = NULL;
}

mẫu
Queue::~Queue()
{
deleteQueue();
}

mẫu
Queue::Queue(const Queue& r)
{
copyQueue(r);
}

mẫu
Queue& Queue::operator=(const Queue& r)
{
if(this != &r)
{
deleteQueue();
copyQueue(r);
}
trả lại *cái này;
}

mẫu
void Queue::copyQueue(const Queue r)
{
front = rear = NULL;
elem_q *p = r.front;
while(p)
{
push(p->inf);
p = p->link;
}
}

mẫu
void Queue::deleteQueue()
{
T x;
while (!empty())
{
pop(x);
}
}

mẫu
bool Queue::empty() const
{
return rear == NULL;
}

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

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

mẫu
void Queue::head(T& x) const
{
if(empty())
{
cout<<"The queue is empty.\n";
}
khác
{
x = front->inf;
}
}

mẫu
void Queue::print()
{
T x;
while(!empty())
{
pop(x);
cout<
}
cout<<>
}

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

}
mẫu
void sortQueue(Queue q,Queue& newq)
{
while(!q.empty())
{
T min;
Queue q1;
minqueue(q , min ,q1);
newq.push(min);
q = q1;
}

}
mẫu
Queue merge(Queue p,Queue q,const T& dummy)
{
p.push(dummy);
q.push(dummy);
Queue r;
T x,y;
p.pop(x);
q.pop(y);
while (!p.empty() && !q.empty())
if (x < y)
{
r.push(x);
p.pop(x);
}
khác
{
r.push(y);
q.pop(y);
}
if (!p.empty())
LÀM
{
r.push(x);
p.pop(x);
}while (x != dummy);
khác
LÀM
{
r.push(y);
q.pop(y);
}while (y != dummy);
trả lại r;
}

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

câu trả lời hay nhất

mẫu 
struct elem_q
{
T inf;
elem_q* link;
};

mẫu
bool operator <( const elem_q &lhs, const elem_q &rhs )
{
return ( lhs.inf < rhs.inf );
}

mẫu
bool operator ==( const elem_q &lhs, const elem_q &rhs )
{
return ( lhs.inf == rhs.inf );
}

mẫu
bool operator !=( const elem_q &lhs, const elem_q &rhs )
{
return ( !( lhs.inf == rhs.inf ) );
}

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

27 4 0
Chứng chỉ ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com
Xem sitemap của VNExpress