Queue

17.5 - Priority queue basic 2


Your next task is to create a program which hires a bunch of mafia from the input and then prints out the hired mafias based on their level from highest to lowest. Use a priority queue for this.

Sample input

Lv. 30 boss
Lv. 60 godfather
Lv. 1 crook
Lv. 15 hitman

Sample output

Lv. 60 godfather
Lv. 30 boss
Lv. 15 hitman
Lv. 1 crook
#include <iostream> #include <string> #include <queue> struct Mafia { int level; std::string title; }; // Define < operator for use inside the priority queue. bool operator<(const Mafia& lhs, const Mafia& rhs) { return lhs.level < rhs.level; } // >> operator definition for inputing a Mafia object. std::istream& operator>>(std::istream &in, Mafia& mafia) { in.ignore(255u, ' '); in >> mafia.level >> mafia.title; return in; } // << operator definition for printing a Mafia object. std::ostream& operator<<(std::ostream &out, const Mafia& mafia) { out << "Lv. " << mafia.level << " " << mafia.title; return out; } int main() { }