×

# priority_queue c++

 2 1 i want to make a min priority_queue of pair integers. but what i want to do is suppose first integer of one of the pair is 10 and second is 20 and that of a another pair first is 10 and second is also 10 i want that if two having equal value of first than than the one having more value of second should be popped first. asked 21 Apr, 00:40 3★anno 162●6 accept rate: 16% given input (10 20), (10 10), (2 10), (1 5), output should be (1 5), (2 10), (10 20), (10 10) (21 Apr, 11:26) anno3★

 1 answered 21 Apr, 11:28 592●8 accept rate: 13% its working .....but can you please explain the logic. That will be very helpful. (21 Apr, 11:40) anno3★ yeah....I got it...Thank You (21 Apr, 11:47) anno3★
 1 Up vote me ... I am not able to ask questions answered 21 Apr, 00:49 19●2 accept rate: 0%
 1 You can perform this by using stl in c++. EDIT : The default comparator for the priority queue is (l < r). You can see that in the constructor default parameter, By modifying the comparator in priority_queue you'd get the desired min priority queue. bool operator() function compares the pairs. You can see that first, it will check whether the first element of pairs is equal or not if it's equal the constructor will do as you want.(Put the pair with a larger second on top().) if not then put the pair with larger first on top(). Here's the code #include "bits/stdc++.h" using namespace std; struct compare { bool operator()(pair p1,pair p2) { if(p1.first == p2.first) return p1.second < p2.second; else return p1.first > p2.first; // ***EDIT*** } }; int main() { priority_queue,vector>,compare> pq; pq.push(make_pair(10,20)); pq.push(make_pair(2,10)); pq.push(make_pair(1,5)); pq.push(make_pair(10,10)); while(!pq.empty()) { pair w = pq.top(); cout << w.first << " " << w.second << endl;; pq.pop(); } }  answered 21 Apr, 01:33 3★only4 1.4k●1●9 accept rate: 18% this will sort only according to first element .... (21 Apr, 07:29) no...its not working...it only sort according to the second element (21 Apr, 10:52) anno3★ give input (10 20), (10 10), (2 10), (1 5), output should be (1 5), (2 10), (10 20), (10 10) (21 Apr, 10:54) anno3★ can you explain how operator works (21 Apr, 13:48) anno3★ Please read the edited answer. (21 Apr, 22:08) only43★
 1 guys, please upvote me. i am new here. nad not able to ask question answered 21 Apr, 03:04 67●2 accept rate: 0%
 0 Priority queue sorts only according to first elements. You can rather create a vector of pairs and sort function does this thing answered 21 Apr, 00:48 19●2 accept rate: 0% even sort function will sort according to first element (21 Apr, 00:58) anno3★ No , sort function sorts a pairs in vector according to the two elements (21 Apr, 07:41)

# include "bits/stdc++.h"

using namespace std;

int main() { priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>> pq; pq.push(make_pair(10,20)); pq.push(make_pair(10,10)); pq.push({10,2}); while(!pq.empty()) { pair<int, int=""> w = pq.top(); cout << w.first << " " << w.second << endl;; pq.pop(); } }

you can declare a priority queue like this and this will do the task for you .

(suspended)
accept rate: 2%

No..it will not work

(21 Apr, 10:32) 3★
 0 Bolanet88.net merupakan situs agen judi bola terpercaya yang menawarkan permainan judi online terlengkap. situs judi bola resmi agen taruhan bola indonesia bandar judi tangkas online uang asli daftar akun sbobet ibcbet answered 21 Apr, 14:24 -1 accept rate: 0%
 toggle preview community wiki

By Email:

Markdown Basics

• *italic* or _italic_
• **bold** or __bold__
• image?![alt text](/path/img.jpg "title")
• numbered list: 1. Foo 2. Bar
• to add a line break simply add two spaces to where you would like the new line to be.
• basic HTML tags are also supported
• mathemetical formulas in Latex between \$ symbol

Tags:

×1,454
×4