I want to insert and order 3 objects (from different classes) in a priority queue.

I create an array to access to the element inside the priority and then I push in the priority queue.

The code:

#include <iostream>
#include <queue>
#include <vector>

using namespace std;

class Alumnos{
		string nombre;
		int edad;
		string operativo;
		Alumnos( string nombre, int edad, string oper ){
			this -> nombre = nombre;
			this -> edad = edad;
			this -> operativo = oper;
		virtual void verificarEdad(){};

class Nuevo : public Alumnos{
		Nuevo( string nombre, int edad, string oper) : Alumnos( nombre,edad, oper ){};
		void verificarEdad(){
			cout << "Nuevo" << endl;

class Medio : public Alumnos{
		Medio( string nombre, int edad, string oper ) : Alumnos ( nombre, edad, oper ){};
		void verificarEdad(){
			cout << "Medio" << endl;

class Viejo : public Alumnos{
		Viejo( string nombre, int edad, string oper ) : Alumnos ( nombre, edad, oper ){};
		void verificarEdad(){
			cout << "Viejo" << endl;

bool operator > (Alumnos alumno1, Alumnos alumno2)
	return alumno1.edad > alumno2.edad;

int main ()
	Nuevo *alumnoNuevo[4];
	Medio *alumnoMedio[4];
	Viejo *alumnoViejo[4];
	priority_queue<Alumnos*, vector<Alumnos*>, greater<vector<Alumnos*>::value_type> > mypq;
	alumnoMedio[0] = new Medio("Helder", 25, "activo");
	alumnoNuevo[0] = new Nuevo("Pepe", 18, "activo");
	alumnoViejo[0] = new Viejo("Pepito", 31, "activo");
	alumnoMedio[1] = new Medio("Juan", 21, "activo");
// 	nuevo n1("Helder", 18);
// 	alumnos &al = n1;
// 	al.verificarEdad();
// 	medio m1("Pepe", 25);
// 	alumnos &al1 = m1;
// 	al1.verificarEdad();


	alumnoMedio[1] -> operativo = "no Activo";
	Alumnos *al = mypq.top();
	for( int i = 0; i < 4; i++ ){
		al -> verificarEdad();
		cout << "mypq.top() is now " << al -> edad << endl;
// 		mypq.push(al);
		al = mypq.top();
*	C A M B I A R  E L  E S T A D O
// 	alumnoMedio[0] -> operativo = "no Activo";
// 	cout << endl << alumnoMedio[0] -> operativo << endl << endl;
	return 0;

The exit is:

mypq.top() is now 25
mypq.top() is now 18
mypq.top() is now 31
mypq.top() is now 21

This is the order I inserted in the queue.

The class operator > is correctly called and the code is correct?
What should I do to insert objects and ordered them with my code?

10 Years
Discussion Span
Last Post by ArkM

Regrettably you did not pay attention to the class Apointer in my post in your previous thread on this topic:
The point is that you have a priority_queue<Alumnos*> now, not priority_queue<Alumnos>. So queue ordering (priority) is defined by operator>() for pointers to Alumnos, not by Alumnos::operator>(). But pointer values ordering does not bear a relation to Alumnos ordering (by edad member values).

Declare wrapper class for pointers to Alumnos with correspondent operator>() and declare priority_queue for this class. Look at my example in the previous thread.


Thanks for your precious help.

I thought I had my problem solved, but now I realized that the operator > in my program doesn't work.

I look at my previous post but I can't understand were I have to touch my code to put working the operator > working.

bool operator > (const Alumnos& alumno1, const Alumnos& alumno2)
	cout << ">" << endl;
	return alumno1.edad > alumno2.edad;

The previous code is my suggestion, but it doesn't work.

I think I have to correct to right way this line or should I change lines more of my code?


Finally I found the solution :D :

I have to declare a struct wich implement the comparison not the operator >.

struct CompareFooPtr : public std::binary_function<Alumnos*, Alumnos*, bool>
	bool operator()(Alumnos* al1, Alumnos* al2)
		return al1 -> edad > al2 -> edad;

priority_queue<Alumnos*, vector<Alumnos*>, CompareFooPtr > mypq;

Many thanks for your help.


Yes, it's one of possible solutions.
It's a matter of tastes: I prefer to declare class wrapper for pointers.
Good luck!

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.