c++ 排序,<< 运算符重载

#include <iostream>
#include <functional>
#include <list>
using namespace std;
ostream& operator<<(ostream& ostr, const list<int>& list)
{
    for (auto &i : list) {
        ostr << "-" << i;
    }
    return ostr;
}
int main()
{
    list<int> list = { 8,7,5,9,0,1,3,2,6,4 };
    
    cout << "before:     " << list << "
";
    list.sort();//升序
    cout << "ascending:  " << list << "
";
    list.sort(greater<int>());//降序
    cout << "descending: " << list << "
";
}

输出

before:     -8-7-5-9-0-1-3-2-6-4
ascending:  -0-1-2-3-4-5-6-7-8-9
descending: -9-8-7-6-5-4-3-2-1-0
原文地址:https://www.cnblogs.com/sea-stream/p/10893013.html