#include <iostream>
#include <list>
#include <iterator>
using namespace std;
bool cmp(int a, int b)
{
return a > b;
}
bool bigger_than_five(int n)
{
return n > 5;
}
int main()
{
list<int> l;
l.push_back(8);
l.push_back(7);
l.push_back(5);
l.push_back(6);
l.push_back(8);
l.sort(cmp); // > 从大到小
//l.sort(greater<int>()); // > 从大到小
copy(l.begin(),l.end(),ostream_iterator<int>(cout," ")); //排序后:88765
cout << endl;
l.remove(8);
copy(l.begin(),l.end(),ostream_iterator<int>(cout," ")); //删除8之后: 765
cout << endl;
l.remove_if(bigger_than_five);
copy(l.begin(),l.end(),ostream_iterator<int>(cout," ")); //删除比5大的数之后:5
cout << endl;
}STL list remove和sort函数
最新推荐文章于 2024-04-07 09:37:09 发布
本文通过一个C++程序示例介绍了如何使用STL中的list容器进行排序、删除元素等操作。具体包括如何自定义比较函数实现逆序排序、删除特定值及删除满足条件的元素。
2万+

被折叠的 条评论
为什么被折叠?



