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

This is a discussion on ow can i use comparison operator in my own Priority Queue Class within the C++ Programming forums, part of the General Programming Boards category; Hi I am in beginner level in C++. I am making my own priority queue class, I have instruction that ...

  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
    21,744
    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?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,650

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, 12:30 AM
  5. gcc problem
    By bjdea1 in forum Linux Programming
    Replies: 13
    Last Post: 04-29-2002, 06:51 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21