일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 | 29 |
30 | 31 |
- qsort
- Articulation_Point
- '0'
- c++
- singly Linked List
- Biconnected_Component
- Algorithm
- Heap
- STL
- Critical_Path_Analysis
- Pair
- sstream
- 예제
- 13305
- template
- 자료구조
- 5397
- 구현
- sort
- red-black tree
- function_template
- data_structure
- 백준
- 총정리
- class_template
- connected_component
- deletion
- 문법
- list
- 알고리즘
- Today
- Total
- Today
- Total
- 방명록
어제의 나보다 성장한 오늘의 나
[C++] Priority Queue & Pair 사용법 본문
Priortity Queue와 Pair를 함께 사용하면 어떤 기능을 할 수 있는지 알아보자.
① 기본적인 사용
첫 번째 인자 기준으로 내림차순 정렬,
같다면 두 번째 인자를 기준으로 내림차순 정렬.
priority_queue<pair<int , int>> pq;
pq.push({1,2});
pq.push({1,3});
pq.push({2,2});
pq.push({2,5});
pq.push({3,1});
while(!pq.empty()){
cout << pq.top().first << pq.top().second << endl;
pq.pop();
}
② greater함수 사용
첫 번째 인자 기준으로 오름차순 정렬,
같다면 두 번째 인자 기준으로 오름차순 정렬.
priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>> pq;
pq.push({1,2});
pq.push({1,3});
pq.push({2,2});
pq.push({2,5});
pq.push({3,1});
while(!pq.empty()){
cout << pq.top().first << pq.top().second << endl;
pq.pop();
}
③ compare 함수 사용
원하는 대로 설정 할 수 있다.
첫 번째 인자 기준으로 내림차순 정렬,
같다면 두 번째 인자 기준으로 오름차순 정렬을 만들어 보았다.
struct cmp{
bool operator()(pair<int, int>&a, pair<int, int>&b) {
if (a.first == b.first) {
return a.second > b.second;
}
else {
return a.first < b.first;
}
}
};
priority_queue<pair<int,int>,vector<pair<int,int>>,cmp> pq;
Pair에 대한 문법
2022.02.10 - [c++/문법] - [C++] Pair 와 Tuple
[C++] Pair 와 Tuple
문제를 풀다보면 연관이 있는 데이터들을 묶어서 사용하고 싶을 때가 있다. 이 때 사용하는 기능이 Pair 와 Tuple이다. Pair는 2쌍 , Tuple은 3쌍 으로 묶을 수 있다. 헤더 파일 Pair -> #in.
8156217.tistory.com
Priority Queue 정리
2022.02.12 - [c++/data_structure_구현] - [c++][자료구조] heap 구현 / STL / Priority Queue 총 정리
[c++][자료구조] heap 구현 / STL / Priority Queue 총 정리
Heap 이란? ◎ complete binary tree (parent node는 2개의 child node를 갖는다.) ◎ parent 와 child 간에 항상 대소 관계가 성립 ◎ parent node가 child node 보다 항상 크면 max h..
8156217.tistory.com