sort を使ってみる (C++)
C++
Published: 2019-12-16

やったこと

sort を使って配列、vector を降順にソートしてみます。

確認環境

$ g++ --version
g++ (Homebrew GCC 9.2.0) 9.2.0
Copyright (C) 2019 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

調査

test.cpp

#include <bits/stdc++.h>
using namespace std;

int main() {
    // 配列
    int a[] = {3, 8, 1};
    sort(a, a + 3, greater<int>());

    for (int i = 0; i < 3; i++) {
        cout << a[i] << endl;
    }

    cout << endl;

    // vector
    vector<int> v = {2, 9, 3};
    sort(v.rbegin(), v.rend());

    for (int i = 0; i < 3; i++) {
        cout << v[i] << endl;
    }
}

出力結果

8
3
1

9
3
2

参考