Priority Queue with Classes

This is a discussion on Priority Queue with Classes within the C++ Programming forums, part of the General Programming Boards category; Hi, I want to set up a priority queue for my class DeformationCandidate and sets the priority based on the ...

  1. #1
    Registered User
    Join Date
    Sep 2006
    Posts
    27

    Priority Queue with Classes

    Hi,

    I want to set up a priority queue for my class DeformationCandidate and sets the priority based on the value of the density variable. I am having a rough time determining the syntax for the queue and am not sure how I need to overload the ==, <, > operators.

    DeformationCandidate.h:
    Code:
    class DeformationCandidate
    {
    public:
    	DeformationCandidate();
    	~DeformationCandidate(){};
    
    	//The row number of the candidate point
    	int row;
    
    	//The column number of the candidate point
    	int column;
    
    	//The tissue density of the candidate point
    	int density;
    
    private:
    
    };
    DeformationCandidate.cpp:
    Code:
    # include "DeformationCandidate.h"
    
    DeformationCandidate::DeformationCandidate()
    {
    	//Initialize variable values
    	row = -1;
    	column = -1;
    	density = -1;
    
    }
    First, how do I declare the queue? Is it:
    Code:
    std::priority_queue<DeformationCandidate> _deformationQueue;
    Second, what do I need to do for the operators?

    I'm having a hard time finding a good resource for priority queues, so please let me know if you have any recommended resources.

    Thanks!
    JackR

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,632
    Quote Originally Posted by JackR
    First, how do I declare the queue?
    Yes, that is correct. However, avoiding prepending an underscore to your variable names. Names that begin with an underscore followed by a lowercase character are reserved to the (language and standard library) implementation for use in the global namespace. Names that begin with an underscore followed by an uppercase character, or that contain two consecutive underscores, are reserved to the implementation for any use.

    Quote Originally Posted by JackR
    Second, what do I need to do for the operators?
    Unless you want to provide a custom comparator, you need to overload operator< for DeformationCandidate in order to store DeformationCandidate objects in a std:riority_queue<DeformationCandidate>. It would probably also make sense to overload operator== in that case.

    You probably should declare the member variables private and provide suitable member functions to form a minimal but complete interface for DeformationCandidate. If you actually want to initialise rather than assign to the member variables, use the constructor's initialisation list, e.g.,
    Code:
    DeformationCandidate::DeformationCandidate() : row(-1), column(-1), density(-1) {}
    Quote Originally Posted by JackR
    I'm having a hard time finding a good resource for priority queues, so please let me know if you have any recommended resources.
    Refer to this Standard Template Library Programmer's Guide and this C++ Reference.
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with FIFO QUEUE
    By jackfraust in forum C++ Programming
    Replies: 23
    Last Post: 04-03-2009, 08:17 AM
  2. Fixing my program
    By Mcwaffle in forum C Programming
    Replies: 5
    Last Post: 11-05-2008, 02:55 AM
  3. Priority Queue Help
    By cjwenigma in forum C++ Programming
    Replies: 6
    Last Post: 11-14-2007, 11:48 PM
  4. help with queues
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 05-21-2002, 09:09 PM
  5. help with queues
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 05-21-2002, 11:39 AM

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