Conversion of priority queue in STL

Source: Internet
Author: User

The queue features FIFO. Generally, queues are similar to queuing for shopping. Everyone is very orderly. the first person in the queue buys something first. The priority queue is different. It does not follow the first-in-first-out rule, but is extracted first with the highest priority based on the priority of the elements in the queue. Generally, the priority queue is equivalent to printing in real life. There are a lot of printers in a print shop, and the performance of each machine is different. Some printers print fast, and some printers print slowly. Wait in the queue when these printers print their tasks in succession. If I want to print a file at this time, I choose not the first printer in the queue, but the best performance and the fastest printer.

Key: priority queue, which depends on the priority. Whoever has a higher priority gets the permission first. No queuing order!

Basic operations:

Empty () returns true if the queue is empty

Pop () deletes the peer Element

Push () to add an element

Size () returns the number of elements in the priority queue.

Top () returns the top-to-top element of the priority queue.

In the default priority queue, a queue with a higher priority is displayed first. In the default int type, a large number is displayed first.

Usage:

Header file:

# Include <queue>

Declaration method:

1. Common Methods:

Priority_queue <int> q;
// The elements are displayed in the ascending order of the elements.

2. Custom priority:

Struct CMP
{
Operator bool () (int x, int y)
{
Return x> Y; // a smaller value of X has a higher priority.
// Other methods can also be written, such as return P [x]> P [y]; indicating that P [I] has a low priority.
}
};
Priority_queue <int, vector <int>, CMP> q; // define a method
// The second parameter indicates the container type. The third parameter is the comparison function.
 

3. struct declaration method:

Struct Node
{
Int X, Y;
Friend bool operator <(node A, Node B)
{
Return A. x> B. X; // In the struct, the priority of X is higher.
}
};
Priority_queue <node> q; // define a method
// In this structure, Y is the value, and X is the priority.
// Compare the priority of an element using the custom operator <operator.
// When "<" is reloaded, it is best not to reload ">". Compilation Error Examples may occur: [CPP]View plaincopy
  1. # Include "iostream"
  2. # Include "vector"
  3. # Include "queue"
  4. Using namespace STD;
  5. Int C [100];
  6. Struct CMP1
  7. {
  8. Bool operator () (int x, int y)
  9. {
  10. Return x> Y; // a small value with a high priority
  11. }
  12. };
  13. Struct cmp2
  14. {
  15. Bool operator () (const int X, const int y)
  16. {
  17. Return C [x]> C [y];
  18. // C [x] has a high priority because the value in the team can be changed externally,
  19. // Therefore, this method does not have a real priority. The struct type is recommended.
  20. }
  21. };
  22. Struct Node
  23. {
  24. Int X, Y;
  25. Friend bool operator <(node A, Node B)
  26. {
  27. Return A. x> B. X; // In the struct, the priority of X is higher.
  28. }
  29. };
  30. Priority_queue <int> q1;
  31. Priority_queue <int, vector <int>, CMP1> Q2;
  32. Priority_queue <int, vector <int>, cmp2> Q3;
  33. Priority_queue <node> Q4;
  34. Queue <int> qq1;
  35. Queue <node> qq2;
  36. Int main ()
  37. {
  38. Int I, J, K, M, N;
  39. Int X, Y;
  40. Node;
  41. While (CIN> N)
  42. {
  43. For (I = 0; I <n; I ++)
  44. {
  45. Cin> A. Y> A. X;
  46. Q4.push ();
  47. }
  48. Cout <Endl;
  49. While (! Q4.empty ())
  50. {
  51. Cout <q4.top (). Y <"" <q4.top (). x <Endl;
  52. Q4.pop ();
  53. }
  54. // Cout <Endl;
  55. }
  56. Return 0;
  57. }

Conversion of priority queue in STL

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.