一个C++list问题
本帖最后由 ~风介~ 于 2016-3-28 17:09 编辑#include<list>
#include<iostream>
using namespace std;
void printlist(const list<int>&listInput);
bool SortPredicate_descending(const int& lsh, const int& rsh);
int main()
{
list<int>list1;
cout<<"please enter 5 numbers: "<<endl;
int x;
for(int i=0;i<5;i++)
{
cin>>x;
list1.push_front(x);
}
cout<<"Initial contents of list are :"<<endl;
printlist(list1);
list1.sort();
cout<<"Order of elements after sort(): "<<endl;
printlist(list1);
list1.sort(SortPredicate_descending);
cout<<"Order of elements after sort() with a predicate: "<<endl;
printlist(list1);
return 0;
}
void printlist(const list<int>&listInput)
{
if(listInput.size()>0)
{
cout<<"{";
list<int>::const_iterator iElementlocator;
for(iElementlocator=listInput.begin();iElementlocator!=listInput.end();++iElementlocator)
cout<<*iElementlocator<<' ';
cout<<"}";
}
else
cout<<"List is empty!"<<endl;
}
bool SortPredicate_descending(const int& lsh, const int& rsh)
{
return(rsh<lsh);
}
只剩一个错误不会改,PS:我用的是VC6.0. vs2010没有错误,没有警告。玩模板建议不要用vc6.0吧,起码不支持c++11标准 我后来在VS试了可以使用。。。
页:
[1]