Example of priority queue in C ++

Source: Internet
Author: User
/* This is a free program, you can modify or redistribute it under the terms of GNU * Description: Example of priority queue usage * language: C ++ * Development Environment: vc6.0 * Author: wangzhicheng * E-mail: 2363702560@qq.com * Date: 2012/10/18 * // * priority_queue priority queue is a one-way queue of the concept of ownership value, in this queue, all elements are sorted by priority. In the specific implementation of STL, priority_queue also uses another container as the bottom structure, then adjust the positions between elements according to the heap processing rules */# include <iostream> # include <queue> # include <string> using namespace STD; const int max = 1000; class person {PRIVATE: int nice; // priority string name; public: Person (INT nice = 0, string name = "") {This-> nice = nice; this-> name = Name;} person (const person & P) {nice = P. nice; name = P. name;} person & operator = (const person & P) {nice = P. nice; name = P. name; return * This;} fr Iend bool operator <(const person & P1, const person & p2) {If (p1.getnice ()! = Maid () {return p1.getnice () <p2.getnice ();} return p1.getname () <p2.getname ();} void setnice (INT nice) {This-> nice = nice;} int getnice () const {return nice;} void setname (string name) {This-> name = Name;} string getname () const {return name;} friend istream & operator> (istream & is, person & P) {int nice; string name; cout <"Enter personnel priority :"; cin> nice; If (! Cin. Good () {cerr <"input error! "<Endl; is. Clear (); return is ;}if (Nice <0 | nice >=max) {cerr <" the input range is incorrect! "<Endl; is. clear (); return is ;}cout <"Enter the person's name:"; CIN >>> name; p. setnice (NICE); p. setname (name); return is;} friend ostream & operator <(ostream & OS, person & P) {OS <"name:" <p. getname () <Endl; OS <"Priority:" <p. getnice () <Endl; return OS ;}}; class lessthan {public: bool operator () (const person & P1, const person & p2) {If (p1.getnice ()! = P2.getnice () {return p1.getnice () <p2.getnice ();} return p1.getname () <p2.getname () ;}}; const int N = 4; void main () {int nice; string name; int I; person P; priority_queue <person, vector <person> q; for (I = 0; I <n; I ++) {cout <"Enter the" <I <"personnel information" <Endl; CIN> P; q. push (p);} while (Q. empty () = false) {cout <q. top (); q. pop ();}}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.