1️⃣ 기본 sort

#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

2️⃣ sort + 람다식 (custom 정렬)

예: 짝수 먼저, 홀수 나중 (오름차순)

#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


예: 구조체나 pair 정렬

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; // 같으면 두 번째 기준 내림차순
});

3️⃣ 우선순위 큐 (priority_queue)

기본 사용

#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