×

priority_queue c++

 2 2 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 '17, 00:40 4★anno 247●10 accept rate: 15% given input (10 20), (10 10), (2 10), (1 5), output should be (1 5), (2 10), (10 20), (10 10) (21 Apr '17, 11:26) anno4★

 1 answered 21 Apr '17, 11:28 846●13 accept rate: 10% its working .....but can you please explain the logic. That will be very helpful. (21 Apr '17, 11:40) anno4★ yeah....I got it...Thank You (21 Apr '17, 11:47) anno4★
 1 Up vote me ... I am not able to ask questions answered 21 Apr '17, 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 '17, 01:33 4★only4 1.5k●2●10 accept rate: 17% this will sort only according to first element .... (21 Apr '17, 07:29) no...its not working...it only sort according to the second element (21 Apr '17, 10:52) anno4★ give input (10 20), (10 10), (2 10), (1 5), output should be (1 5), (2 10), (10 20), (10 10) (21 Apr '17, 10:54) anno4★ can you explain how operator works (21 Apr '17, 13:48) anno4★ Please read the edited answer. (21 Apr '17, 22:08) only44★
 1 guys, please upvote me. i am new here. nad not able to ask question answered 21 Apr '17, 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 '17, 00:48 19●2 accept rate: 0% even sort function will sort according to first element (21 Apr '17, 00:58) anno4★ No , sort function sorts a pairs in vector according to the two elements (21 Apr '17, 07:41)
 0 Answer is hidden as author is suspended. Click here to view. answered 21 Apr '17, 07:48 (suspended) accept rate: 2% No..it will not work (21 Apr '17, 10:32) anno4★
 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 '17, 14:24 -1 accept rate: 0%
 toggle preview community wiki:
Preview

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

Question tags:

×1,720
×9

question asked: 21 Apr '17, 00:40

question was seen: 2,459 times

last updated: 21 Apr '17, 22:08