-
์ฐ์ ์์ํ์ฝ๋ฉํ ์คํธ/์ฝ๋ฉํ ์คํธ ํ 2020. 9. 6. 04:48728x90
#include<iostream> #include<queue> using namespace std; struct Coord { int x; int y; Coord(int _x, int _y) { x = _x; y = _y; } }; struct Comp { // return ์ด < ์ด๋ฉด ๋์ ์์ผ๋ก ๋ฝ๊ณ (๋ด๋ฆผ์ฐจ์), > ๋ฎ์ ์์ผ๋ก ๋ฝ๋๋ค(์ค๋ฆ์ฐจ์) bool operator()(struct Coord a, struct Coord b) { if (a.x == b.x) return a.y < b.y; // x๊ฐ ๊ฐ์ ๊ฒฝ์ฐ y๋ ๋ด๋ฆผ์ฐจ์ return a.x > b.x; // x๋ ์ค๋ฆ์ฐจ์ } }; int main() { priority_queue<int> pq_1; // ์ฐ์ ์์ํ๋ ๊ธฐ๋ณธ์ ์ผ๋ก ๋ด๋ฆผ์ฐจ์ pq_1.push(5); pq_1.push(2); while (!pq_1.empty()) { cout << pq_1.top() << endl; pq_1.pop(); } priority_queue<int, vector<int>, greater<int>> pq_2; // ์ค๋ฆ์ฐจ์์ผ๋ก ์์ฑ pq_2.push(5); pq_2.push(2); while (!pq_2.empty()) { cout << pq_2.top() << endl; pq_2.pop(); } priority_queue<Coord, vector<Coord>, Comp> pq_3; // ์กฐ๊ฑด์ ๋ด๊ฐ ์ค์ Coord test01(2, 3); Coord test02(5, 7); Coord test03(2, 8); Coord test04(5, 4); pq_3.push(test01); pq_3.push(test02); pq_3.push(test03); pq_3.push(test04); while (!pq_3.empty()) { cout << pq_3.top().x << " " << pq_3.top().y << endl; pq_3.pop(); } return 0; }
728x90'์ฝ๋ฉํ ์คํธ > ์ฝ๋ฉํ ์คํธ ํ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
๋ฌธ์์ด ์ฒ๋ฆฌ (0) 2020.09.08 ์์์ ์๋ฆฌ ์ถ๋ ฅ (0) 2020.08.28 ๋ณ์ ํ์ ๋ณํ (0) 2020.08.28 C++ ๋ฌธ์์ด ์๋ฅด๊ธฐ (0) 2020.04.09 ์ฝํ ํ๋ฉด์ ํท๊น๋ฆฌ๋ ๊ฒ๋ค (0) 2020.04.05