std::list::sort

提供: C++入門
移動: 案内検索
スポンサーリンク

std::list::sort とは、std::listをソートする機能を提供します。昇順、降順で並べたり、任意の比較関数を指定して並べ替えることが可能です。

読み方

std::list::sort
えすてぃーでぃー りすと そーと

概要

std::vectorだとstd::sortを使用したりしますが、std::listでは、std::list::sortを使用して、ソートします。

昇順(0,1,2,3...)にソートするには、以下の通りです。

list<int> list1 = { 3,1,2 };
list1.sort();

降順(3,2,1)にソートするには、以下の通りです。std::greaterを使用します。

list<int> list1 = { 3,1,2 };
list1.sort(std::greater<int>());

整数リストのソートの例 list_sort1.cpp

ソースコード list_sort1.cpp

/*
 * list1.cpp
 * Copyright (C) 2014 kaoru <kaoru@bsd>
 */
 
#include <iostream>
#include <list>
#include <functional>
using namespace std;
 
void
print (list<int> &c) {
        for (auto x: c) {
                cout << x << " ";
        }
        cout << endl;
}
 
int
main(int argc, char const* argv[])
{
        list<int> list1 = { 3,1,2 };
 
        cout << "Before sort: ";
        print(list1); // 3 1 2
 
        list1.sort();
        cout << "After sort: ";
        print(list1); // 1 2 3
 
        list1.sort(std::greater<int>());
        cout << "After sort(greater): ";
        print(list1); // 3 2 1
 
        return 0;
}

コンパイル

g++49 -std=c++11 -I/usr/local/lib/gcc49/include/c++/ \
-Wl,-rpath=/usr/local/lib/gcc49  list_sort1.cpp -o list_sort1

実行例

% ./list_sort1
Before sort: 3 1 2
After sort: 1 2 3
After sort(greater): 3 2 1

list_sort_string1.cpp の例

ソースコード list_sort_string1.cpp

/*
 * list1.cpp
 * Copyright (C) 2014 kaoru <kaoru@bsd>
 */
 
#include <iostream>
#include <list>
#include <functional>
#include <string>
using namespace std;
 
void
print (list<string> &c) {
        for (auto x: c) {
                cout << x << " ";
        }
        cout << endl;
}
 
int
main(int argc, char const* argv[])
{
        list<string> list1 = { "xzy", "abc","def" };
 
        cout << "Before sort: ";
        print(list1);
 
        list1.sort();
        cout << "After sort: ";
        print(list1);
 
        list1.sort(std::greater<string>());
        cout << "After sort(greater): ";
        print(list1);
 
        return 0;
}

コンパイル

g++49 -std=c++11 -I/usr/local/lib/gcc49/include/c++/ \
-Wl,-rpath=/usr/local/lib/gcc49  list_sort_string1.cpp -o list_sort_string1

実行例

% ./list_sort_string1
Before sort: xzy abc def
After sort: abc def xzy
After sort(greater): xzy def abc

文字列のリストのソートの例 list_sort_string1.cpp

ソースコード list_sort_string1.cpp

/*
 * list1.cpp
 * Copyright (C) 2014 kaoru <kaoru@bsd>
 */
 
#include <iostream>
#include <list>
#include <functional>
#include <string>
using namespace std;
 
void
print (list<string> &c) {
        for (auto x: c) {
                cout << x << " ";
        }
        cout << endl;
}
 
int
main(int argc, char const* argv[])
{
        list<string> list1 = { "xzy", "abc","def" };
 
        cout << "Before sort: ";
        print(list1);
 
        list1.sort();
        cout << "After sort: ";
        print(list1);
 
        list1.sort(std::greater<string>());
        cout << "After sort(greater): ";
        print(list1);
 
        return 0;
}

コンパイル

g++49 -std=c++11 -I/usr/local/lib/gcc49/include/c++/ \
-Wl,-rpath=/usr/local/lib/gcc49  list_sort_string1.cpp -o list_sort_string1

実行例

% ./list_sort_string1
Before sort: xzy abc def
After sort: abc def xzy
After sort(greater): xzy def abc

構造体やクラスのリストをソートする例 list_sort_compare1.cpp

この例は、クラスでの例ですが、構造体もやり方は同じです。

ソースコード list_sort_compare1.cpp

/*
 * list1.cpp
 * Copyright (C) 2014 kaoru <kaoru@bsd>
 */
 
#include <iostream>
#include <list>
#include <functional>
using namespace std;
 
class C {
        public:
        int _x;
        int _y;
        C(int x, int y): _x(x), _y(y) {
        }
        virtual ~C() {}
 
        friend ostream& operator<<(ostream& os, const C& v) {
                os << v._x << "," << v._y;
                return os;
        }
};
 
bool comp(C& v1, C& v2) {
        if (v1._x > v2._x) {
                return true;
        } else {
                return (v1._y > v2._y);
        }
 
        // NOTREACHED
        return false;
}
 
void
print (list<C> &c) {
        for (auto x: c) {
                cout << x << " ";
        }
        cout << endl;
}
 
int
main(int argc, char const* argv[])
{
        list<C> list1 = {
                C(1, 3),
                C(2, 5),
                C(3, 5),
                C(3, 2)
        };
 
        cout << "Before sort: ";
        print(list1);
 
        list1.sort(comp);
        cout << "After sort: ";
        print(list1);
 
        return 0;
}

コンパイル

g++49 -std=c++11 -I/usr/local/lib/gcc49/include/c++/ \
-Wl,-rpath=/usr/local/lib/gcc49  list_sort_compare1.cpp -o list_sort_compare1

実行例

% ./list_sort_compare1
Before sort: 1,3 2,5 3,5 3,2
After sort: 3,5 3,2 2,5 1,3

関連項目




スポンサーリンク