Thread: priority_queue

  1. #1
    Registered User
    Join Date
    Aug 2006
    Posts
    43

    priority_queue

    I'm looking to create a priority_queue with the vector created in main() using B::weight to order the queue. How might I go about doing that?

    Code:
    #include <vector>
    #include <string>
    
    template <typename T>
    class A
    {
    public:
    	class B
    	{
    	private:
    		B()
    		{
    			weight = 0;
    		}
    		T data;
    		int weight;
    		friend class A;
    	public:
    		int get_weight()
    		{
    			return weight;
    		}
    		
    		bool operator==(const B& rhs) const
    		{
    			return (*this).weight == rhs.weight;
    		}
    
    		bool operator<(const B& rhs) const
    		{
    			return (*this).weight < rhs.weight;
    		}
    
    		bool operator>(const B& rhs) const
    		{
    			return (*this).weight > rhs.weight;
    		}
    	};
    	A() {}
    	virtual ~A() {}
    	B* create_B(T data, int weight = 0);
    };
    
    template <typename T>
    typename A<T>::B* A<T>::create_B(T data, int weight = 0)
    {
    	B* new_B = new B;
    	new_B->data = data;
    	new_B->weight = weight;
    	return new_B;
    }
    
    int main()
    {
    	A<std::string> myA;
    	std::vector<A<std::string>::B*> stuff;
    	stuff.push_back(myA.create_B("string1", 1));
    	stuff.push_back(myA.create_B("string2", 2));
    
    	return 0;
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    It looks like operator< is already correctly defined so you can #include <queue> and use a std::priority_queue<A<std::string>::B>.
    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
    Registered User
    Join Date
    Aug 2006
    Posts
    43
    That works great, but I need the queue to hold type B*, not B, so:
    Code:
    std::priority_queue<A<std::string>::B*>
    The problem is, I still need it to sort using operator< (or B::weight). How would I go about doing such a thing?

  4. #4
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    make a wrapper class perhaps?

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Or write a custom sort function object that sorts objects of type B* by referring to the objects to which they point.

  6. #6
    Registered User
    Join Date
    Aug 2006
    Posts
    43
    Quote Originally Posted by tabstop View Post
    Or write a custom sort function object that sorts objects of type B* by referring to the objects to which they point.
    I'm assuming, then, that I couldn't convince std::sort() to do the job on a vector of B*?

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Of course, you can. But remember what sort looks like:
    Code:
    void sort ( RandomAccessIterator first, RandomAccessIterator last, Compare comp );
    You need to tell sort when things are less than other things.

Popular pages Recent additions subscribe to a feed