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;
}