priority_queue

This is a discussion on priority_queue within the C++ Programming forums, part of the General Programming Boards category; I'm looking to create a priority_queue with the vector created in main() using B::weight to order the queue. How might ...

  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
    21,412
    It looks like operator< is already correctly defined so you can #include <queue> and use a std::priority_queue<A<std::string>::B>.
    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
    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,167
    make a wrapper class perhaps?

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    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,185
    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

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