#include<iostream>
using namespace std;
#include<list>
#include<time.h>
#include<algorithm>
void print(const list<int> l)
{
for (list<int>::const_iterator it = l.begin(); it != l.end(); it++)
{
cout << (*it) << " ";
}
cout << endl;
}
bool MyCompare(int v1, int v2)
{
return v1 > v2;
}
void test0701()
{
//反转
list<int> l1;
for (int i = 0; i < 10; i++)
{
l1.push_back(rand() % 101);
}
cout << "反转前:" << endl;
print(l1);
cout << "反转后:" << endl;
l1.reverse();
print(l1);
//排序前
cout << "排序前:" << endl;
print(l1);
//排序后
cout << "排序后:" << endl;
//sort(l1.begin(), l1.end());//所有不支持随机访问迭代器的容器,不可以用标准算法
//不支持随机访问迭代器的容器,内部会提供对应的一些算法
l1.sort();//默认升序排序
print(l1);
//降序排序
l1.sort(MyCompare);
print(l1);
}
int main()
{
srand((unsigned int)time(NULL));
test0701();
system("pause");
return 0;
}
网友评论