C++ (STL) Sort


Sort


実際の符号化テストでは,ソートを作成して書くことはほとんどなかった.
コンセプトに精通し、実際にSTLのSortライブラリを使用すればよい.

[使用]


(基本的な使い方)
#include <iostream>
#include <algorithm>

using namespace std;

int main(void){
   int a[10] = {9, 3, 5, 4, 1, 10, 8, 6, 7, 2};
   sort(a, a+10); // sort 사용!
}
(所定の条件でソート)
#include <iostream>
#include <algorithm>

using namespace std;

bool compare(int a, int b){
	return a > b;
}
int main(void){
   int a[10] = {9, 3, 5, 4, 1, 10, 8, 6, 7, 2};
   sort(a, a+10, compare); // 조건에 따라 내림차순 정렬이 수행된다
}
(データのグループ化とソート方法)-より現実的な方法を使用する方法-pairを使用します!
#include <iostream>
#include <algorithm>

using namespace std;

class Student{
public:
    string name;
    int score;
    Student(string name, int score){
        this->name = name;
        this->score = score;
    }
    // 정렬 기준은 '점수가 작은 순서'
    bool operator < (const Student &student){
        return this->score < student.score;
    }
};

---
int main(){
     Student students[] = {
        Student("김정욱", 93),
        Student("한재현", 92),
        Student("강병헌", 91),
        Student("이현종", 87),
        Student("이진호", 94),
    };
    sort(students, students+5);
    for (auto a : students){
        cout << a.name << endl;
    }
}
(データのグループ化とソート方法)-適切なカテゴリを選択する方法(pairを使用)
#include <iostream>
#include <algorithm>
#include <vector>

using namespace std;

int main(){
    vector <pair<int, string>> v;
    v.push_back({95, "김정욱"});
    v.push_back({91, "한재현"});
    v.push_back({94, "김가영"});
    v.push_back({93, "주주베"});

    sort(v.begin(), v.end());

    for(auto a : v){
        cout << a.second << ' ';
    }
}
(pairに比較条件を追加してソート)
#include <iostream>
#include <algorithm>
#include <vector>

using namespace std;

bool compare(pair<string, pair<int, int>> a, pair<string, pair<int, int>> b){
    if(a.second.first == b.second.first){
        return a.second.second > b.second.second;
    }else{
        return a.second.first > b.second.first;
    }
}

int main(){
    vector <pair<string, pair<int, int>>> v;
    v.push_back({"김정욱", {91, 19960808}});
    v.push_back({"한재현", {93, 19960921}});
    v.push_back({"김가영", {91, 19960807}});
    v.push_back({"주주베", {96, 19961011}});

    sort(v.begin(), v.end(), compare);

    for(auto a : v){
        cout << a.first << ' ';
    }
}

BOJ : 1181


:STL sort()を使用して問題をソート
#include <iostream>
#include <algorithm>

using namespace std;

bool compare(string a, string b){
    if(a.length() < b.length()){
        return 1;
    }else if(a.length() > b.length()){
        return 0;
    }else{
        return a < b;
    }
}

int main()
{
    int N;
    cin >> N;
    string str[20002];
    for(int i=0;i<N;i++){
        cin >> str[i];
    }
    sort(str, str+N, compare);
    for(int i=0;i<N;i++)
    {
        if(i>0 && str[i-1] == str[i]){
            continue;
        }else{
            cout << str[i] <<'\n';
        }
    }
}

BOJ : 1431


c++STL sortを使用して問題をソート(2)

[コード]

#include <iostream>
#include <algorithm>

using namespace std;

bool compare(string a, string b){
    if(a.length() < b.length()){
        return 1;
    }else if(a.length() > b.length()){
        return 0;
    }
        int sumA=0, sumB=0;
        for(auto i : a){
            if(i >= '0' && i <= '9'){
                sumA+= i-'0';
            }
        }
        for(auto i : b){
            if(i >= '0' && i <= '9'){
                sumB+= i-'0';
            }
        }
        if(sumA < sumB){
            return 1;
        }else if(sumA > sumB){
            return 0;
        }else{
            return a < b;
        }
}

int main()
{
    int N;
    cin >> N;
    string str[1002];
    for(int i=0;i<N;i++){
        cin >> str[i];
    }
    sort(str, str+N, compare);
    for(int i=0;i<N;i++){
        cout << str[i] << '\n';
    }
}