sort

STL sort 예제이다.

pair sort

첫번째 원소를 기준으로 정렬한다.
첫번쩨 원소가 같으면 두번째 원소를 기준으로 정렬한다.

#include <iostream>
#include <vector>
#include <algorithm>
#include <string>

using namespace std;

int main()
{
    vector<pair<int, string>> v;
    v.push_back(pair<int, string>(30, "kim"));
    v.push_back(pair<int, string>(10, "park"));
    v.push_back(pair<int, string>(20, "choi"));
    v.push_back(pair<int, string>(20, "lee"));
    v.push_back(pair<int, string>(50, "han"));

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

    for (auto node : v)
    {
        cout << node.first << " : " << node.second << endl;
    }

    return 0;
}

/*
실행 결과

10 : park
20 : choi
20 : lee
30 : kim
50 : han
*/

클래스 < 연산자 비교

클래스 < 연산자 오버로딩을 통해서 비교한다.
std::sort 함수는 원소 비교에 '<' 연산자를 사용한다.

#include <iostream>
#include <vector>
#include <algorithm>
#include <string>

using namespace std;

class Student
{
public:
    int index;
    string name;
    Student(int index, string name)
    {
        this->index = index;
        this->name = name;
    }

    bool operator < (const Student& rha)
    {
        return this->index < rha.index;
    }
};

int main()
{
    vector<Student> v;
    v.push_back(Student(30, "kim"));
    v.push_back(Student(10, "park"));
    v.push_back(Student(20, "choi"));
    v.push_back(Student(20, "lee"));
    v.push_back(Student(50, "han"));

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

    for (auto node : v)
    {
        cout << node.index << " : " << node.name << endl;
    }

    return 0;
}

/*
실행 결과

10 : park
20 : choi
20 : lee
30 : kim
50 : han
*/

클래스 외부 < 연산자 비교

함수  < 연산자 오버로딩을 통해서 비교한다.

#include <iostream>
#include <vector>
#include <algorithm>
#include <string>

using namespace std;

class Student
{
public:
    int index;
    string name;
    Student(int index, string name)
    {
        this->index = index;
        this->name = name;
    }
};


bool operator < (const Student& l, const Student& r)
{
    return l.index < r.index;
}

int main()
{
    vector<Student> v;
    v.push_back(Student(30, "kim"));
    v.push_back(Student(10, "park"));
    v.push_back(Student(20, "choi"));
    v.push_back(Student(20, "lee"));
    v.push_back(Student(50, "han"));

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

    for (auto node : v)
    {
        cout << node.index << " : " << node.name << endl;
    }

    return 0;
}

클래스 외부 < 연산자 비교 둘

함수 통해서 비교한다.

#include <iostream>
#include <vector>
#include <algorithm>
#include <string>

using namespace std;

class Student
{
public:
    int index;
    string name;
    Student(int index, string name)
    {
        this->index = index;
        this->name = name;
    }
};


bool compare(const Student& l, const Student& r)
{
    return l.index < r.index; 
}

int main()
{
    vector<Student> v;
    v.push_back(Student(30, "kim"));
    v.push_back(Student(10, "park"));
    v.push_back(Student(20, "choi"));
    v.push_back(Student(20, "lee"));
    v.push_back(Student(50, "han"));

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

    for (auto node : v)
    {
        cout << node.index << " : " << node.name << endl;
    }

    return 0;
}

람다를 이용한 비교

class Student
{
public:
    int index;
    string name;
    Student(int index, string name)
    {
        this->index = index;
        this->name = name;
    }
};

int main()
{
    vector<Student> v;
    v.push_back(Student(30, "kim"));
    v.push_back(Student(10, "park"));
    v.push_back(Student(20, "choi"));
    v.push_back(Student(20, "lee"));
    v.push_back(Student(50, "han"));

    auto compare = [](const Student& l, const Student& r) -> bool
    {
        return l.index < r.index;
    };
   
    sort(v.begin(), v.end(), compare);

    for (auto node : v)
    {
        cout << node.index << " : " << node.name << endl;
    }

    return 0;
}

람다를 이용한 문자열 비교

#include <iostream>
#include <vector>
#include <algorithm>
#include <string>

using namespace std;

class Student
{
public:
    int index;
    string name;
    Student(int index, string name)
    {
        this->index = index;
        this->name = name;
    }
};

int main()
{
    vector<Student> v;
    v.push_back(Student(30, "kim"));
    v.push_back(Student(10, "park"));
    v.push_back(Student(20, "choi"));
    v.push_back(Student(20, "lee"));
    v.push_back(Student(50, "han"));

    bool isNumber = false;
    auto compare = [isNumber](const Student& l, const Student& r) -> bool
    {
        if(isNumber)
            return l.index < r.index;
        else
            return l.name.compare(r.name) < 0;
    };
   
    sort(v.begin(), v.end(), compare);

    for (auto node : v)
    {
        cout << node.index << " : " << node.name << endl;
    }

    return 0;
}

/*
20 : choi
50 : han
30 : kim
20 : lee
10 : park
*/

람다를 이용한 내림차순 비교

#include <iostream>
#include <vector>
#include <algorithm>
#include <string>

using namespace std;

class Student
{
public:
    int index;
    string name;
    Student(int index, string name)
    {
        this->index = index;
        this->name = name;
    }
};

int main()
{
    vector<Student*> v;
    v.push_back(Student(30, "kim"));
    v.push_back(Student(10, "park"));
    v.push_back(Student(20, "choi"));
    v.push_back(Student(20, "lee"));
    v.push_back(Student(50, "han"));

    auto rcompare = [](const Student& l, const Student& r) -> bool
    {
        return l.index > r.index;
    };
   
    sort(v.begin(), v.end(), rcompare);

    for (auto node : v)
    {
        cout << node.index << " : " << node.name << endl;
    }

    return 0;
}

/*
50 : han
30 : kim
20 : choi
20 : lee
10 : park
*/

오브젝트 포인터 소팅

< 연산자 비교는 빌드는 되지만 실행이 안된다.
비교 함수를 이용해야 한다.

#include <iostream>
#include <vector>
#include <algorithm>
#include <string>

using namespace std;

class Student
{
public:
    int index;
    string name;
    Student(int index, string name)
    {
        this->index = index;
        this->name = name;
    }

    /*
    호출이 안된다
    bool operator<(const Student *t) const {
        return(index < t->index);
    }
    */
};

int main()
{
    vector<Student*> v;
    v.push_back(new Student(30, "kim"));
    v.push_back(new Student(10, "park"));
    v.push_back(new Student(20, "choi"));
    v.push_back(new Student(20, "lee"));
    v.push_back(new Student(50, "han"));

    auto compare = [](const Student* l, const Student* r) -> bool
    {
        return l->index < r->index;
    };
   
    sort(v.begin(), v.end(), compare);

    for (auto node : v)
    {
        cout << node->index << " : " << node->name << endl;
    }
    return 0;
}

unique_ptr 오브젝트 포인터 소팅

#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <memory>

using namespace std;

class Student
{
public:
    int index;
    string name;
    Student(int index, string name)
    {
        this->index = index;
        this->name = name;
    }
};

int main()
{
    vector<unique_ptr<Student>> v;
    v.push_back(unique_ptr<Student>(new Student(30, "kim")));
    v.push_back(unique_ptr<Student>(new Student(10, "park")));
    v.push_back(unique_ptr<Student>(new Student(20, "choi")));
    v.push_back(unique_ptr<Student>(new Student(20, "lee")));
    v.push_back(unique_ptr<Student>(new Student(50, "han")));
   
    auto compare = [](const unique_ptr<Student> &l, const unique_ptr<Student> &r) -> bool
    {
        return l->index < r->index;
    };
   
    sort(v.begin(), v.end(), compare);
   
    for (const auto& node : v)
    {
        cout << node->index << " : " << node->name << endl;
    }
    return 0;
}