Thread: ow can i use comparison operator in my own Priority Queue Class

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    1

    Unhappy ow can i use comparison operator in my own Priority Queue Class

    Hi I am in beginner level in C++. I am making my own priority queue class, I have instruction that I want to use the comparison operator(operator <) for determine best priority. So how can I use that in my priority class???.Here just I tried:

    Code:
    template<typename T>
    class MyPQ
    {
    public:
    	vector<T> vecImp;
    	MyPQ()
    	{
    	    vecImp.empty();
    	}
    	MyPQ(T &data)
    	{
    		push(data);
    	}
    
    	bool empty() const
    	{
    		if(vecImp.empty())
    		{
    			return true;
    		}else
    		{
    			return false;
    		}
    	}
    
    	void push(const T &data)
    	{
    		insertData(data);
    		vecImp.push_back(data);
    	}
    
    
    	T pop()
    	{
    	// pop code
    
    	}
    	
    	T getData()
    	{
    		return data;
    	}
    	
    	void insertData(const T& insert)
    	{
    		data=insert;
    	}
    
    	bool operator< (const MyPQ& Q1)
    	{
    		return data < Q1.data;
    	}
    
    	~MyPQ()
    	{
    
    	}
    
    private:
    	T data;
    };
    So my problem where I can use the comparison method? in pop() method? or in the main routine?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    How exactly do you plan to implement the "priority" aspect of your priority queue? For example, do you intend to keep your vector sorted when pushing, or do you intend to linear search for and remove the smallest/largest element when popping, or have you been told about the heap data structure?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Lost on Queues - Help GREATLY appreciated.
    By TetsuoShima in forum C Programming
    Replies: 3
    Last Post: 07-13-2010, 05:35 AM
  2. Priority queue not processed in expected order
    By JackR in forum C++ Programming
    Replies: 2
    Last Post: 10-27-2009, 08:17 AM
  3. Priority Queue with Classes
    By JackR in forum C++ Programming
    Replies: 1
    Last Post: 10-06-2009, 12:42 AM
  4. Priority queue
    By cjwenigma in forum C++ Programming
    Replies: 6
    Last Post: 12-03-2007, 01:30 AM
  5. gcc problem
    By bjdea1 in forum Linux Programming
    Replies: 13
    Last Post: 04-29-2002, 06:51 PM