list和forward_list有成员sort()函数
set/map自动排序
array,vector,deque用全局sort()
代码示例:
void main()
{
list<int> m_list{ 1, 3, 5, 7, 9, 2, 4, 6, 8, 10 };
cout << "m_list从大到小:";
m_list.sort([](int x, int y){return x > y; });//从大到小
for (auto node :m_list)
{
cout << node << " ";
}
cout << endl;
forward_list<int> m_forwardlist{ 1, 3, 5, 7, 9, 2, 4, 6, 8, 10 };
cout << "m_forwardlist从小到大:";
m_forwardlist.sort([](int x, int y){return x < y; });
for (auto node :m_forwardlist)
{
cout << node << " ";
}
cout << endl;
vector<int> m_vc{ 1, 3, 5, 7, 9, 2, 4, 6, 8, 10 };
cout << "m_vc使用全局sort函数:";
sort(m_vc.begin(), m_vc.end(), [](int x, int y){return x < y; });
for (auto node : m_vc)
{
cout << node << " ";
}
cout << endl;
array<int,9> m_array = { 1, 3, 5, 7, 9, 2, 4, 6, 8 };
cout << "m_array使用全局sort函数:";
sort(m_array.begin(), m_array.end(), [](int x, int y){return x > y; });
for (auto node : m_array)
{
cout << node << " ";
}
system("pause");
}
结果: