#include <bits/stdc++.h>
using namespace std;
int main() {
vector<int> v = {5, 2, 8, 1, 3};
sort(v.begin(), v.end()); // 오름차순
for (int x : v) cout << x << " ";
cout << "\\n";
sort(v.begin(), v.end(), greater<int>()); // 내림차순
for (int x : v) cout << x << " ";
cout << "\\n";
}
📘 출력:
1 2 3 5 8
8 5 3 2 1
#include <bits/stdc++.h>
using namespace std;
int main() {
vector<int> v = {5, 2, 8, 1, 3};
sort(v.begin(), v.end(), [](int a, int b) {
if (a % 2 != b % 2) return a % 2 == 0; // 짝수 우선
return a < b; // 같으면 오름차순
});
for (int x : v) cout << x << " ";
}
📘 출력:
2 8 1 3 5
람다식 구조:
[](자료형 a, 자료형 b){ return bool; }
→ true면 a가 b보다 앞으로 감
vector<pair<int,int>> vp = {{1,5},{2,2},{1,3}};
sort(vp.begin(), vp.end(), [](auto &a, auto &b){
if (a.first != b.first) return a.first < b.first; // 첫 번째 기준
return a.second > b.second; // 같으면 두 번째 기준 내림차순
});
#include <bits/stdc++.h>
using namespace std;
int main() {
priority_queue<int> pq; // 최대힙 (default)
pq.push(5);
pq.push(2);
pq.push(8);
while(!pq.empty()){
cout << pq.top() << " "; // 항상 최대값
pq.pop();
}
}
📘 출력:
8 5 2