c ++重新定义运算符<()和operator!=()

时间:2014-02-06 21:55:45

标签: c++ operator-overloading operator-keyword

我有这个队列的实现:

#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;
}

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

3 个答案:

答案 0 :(得分:1)

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 ) );
} 

答案 1 :(得分:0)

看看你是如何做到的:

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

重载您需要的其他操作符。

答案 2 :(得分:0)

嗯,逻辑运算符具有相当期望的语法,如下所示:

  bool ClassName::operator!=(const ClassName& other) const {
      return //compare as apropriate.
  }

但是,我必须注意两件事:

  • 首先,你真的需要再次完成课程设计 - &gt;使用引用时有很多不一致的地方,奇怪的API,我甚至不确定minqueue究竟应该做什么,或者更确切地知道它为什么会这样做。
  • 其次,如果你想让队列自行排序,从最小元素开始,你应该查找优先级队列或堆。