// ListDemo.cpp : 定义控制台应用程序的入口点。
// Visual C++ 6.0 下编译通过
# include <list>
# include <iostream>
using namespace std;
class IntListDemo
{
public:
typedef list<int> INTLIST;
IntListDemo();
~IntListDemo();
void PushBackDemo(void);
void PopBackDemo(void);
void PrintSizeMaxsizeDemo(void);
void ReverseDemo(void);
void ResizeDemo(int size);
void BeginDemo(void);
void EndDemo(void);
void BackDemo(void);
void FrontDemo(void);
void AssignDemo(void);
void InsertDemo(void);
void SortDemo(void);
void UniqueDemo(void);
void RemoveDemo(void);
void PrintIntList(void);
private:
INTLIST iList;
};
IntListDemo::IntListDemo()
{
}
IntListDemo::~IntListDemo()
{
}
void IntListDemo::PrintIntList(void)
{
cout<<"List:[";
for(INTLIST::iterator iter = iList.begin(); iter != iList.end(); iter++)
{
cout<<*iter;
if(iter != (--iList.end()))
{
cout<<",";
}
}
cout<<"]"<<endl;;
}
void IntListDemo::PushBackDemo(void)
{
iList.push_back(2);
iList.push_back(3);
iList.push_back(4);
iList.push_back(1);
iList.push_back(20);
PrintIntList();
}
void IntListDemo::PopBackDemo(void)
{
iList.pop_back();
PrintIntList();
}
void IntListDemo::PrintSizeMaxsizeDemo(void)
{
cout<<"Size="<<iList.size();
cout<<" Maxsize="<<iList.max_size()<<endl;
}
void IntListDemo::ReverseDemo(void)
{
iList.reverse();
cout<<"After reverse:";
PrintIntList();
}
void IntListDemo::ResizeDemo(int size)
{
iList.resize(size);
cout<<"After resize:";
PrintSizeMaxsizeDemo();
}
void IntListDemo::BeginDemo(void)
{
cout<<"First element:"<<*(iList.begin())<<endl;
}
void IntListDemo::EndDemo(void)
{
cout<<"Last element:"<<*(--iList.end())<<endl;
}
void IntListDemo::FrontDemo(void)
{
cout<<"First element:"<<iList.front()<<endl;
}
void IntListDemo::BackDemo(void)
{
cout<<"Last element:"<<iList.back()<<endl;
}
void IntListDemo::AssignDemo(void)
{
iList.assign(5, 8);
PrintIntList();
}
void IntListDemo::InsertDemo(void)
{
iList.insert(iList.begin(), 3, 10);
PrintIntList();
}
void IntListDemo::SortDemo(void)
{
cout<<"After sort:";
iList.sort();
PrintIntList();
}
void IntListDemo::UniqueDemo(void)
{
cout<<"After unique operator:";
iList.unique();
PrintIntList();
}
void IntListDemo::RemoveDemo(void)
{
cout<<"After remove 8:";
iList.remove(8);
PrintIntList();
}
int main(int argc, char* argv[])
{
IntListDemo iListDemo;
iListDemo.PushBackDemo();
iListDemo.PopBackDemo();
iListDemo.PrintSizeMaxsizeDemo();
iListDemo.ReverseDemo();
iListDemo.ResizeDemo(50);
iListDemo.BeginDemo();
iListDemo.EndDemo();
iListDemo.FrontDemo();
iListDemo.BackDemo();
iListDemo.SortDemo();
iListDemo.AssignDemo();
iListDemo.InsertDemo();
iListDemo.UniqueDemo();
iListDemo.RemoveDemo();
return 0;
}
//输出:
List:[2,3,4,1,20]
List:[2,3,4,1]
Size=4 Maxsize=1073741823
After reverse:List:[1,4,3,2]
After resize:Size=50 Maxsize=1073741823
First element:1
Last element:0
First element:1
Last element:0
After sort:List:[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,2,3,4]
List:[8,8,8,8,8]
List:[10,10,10,8,8,8,8,8]
After unique operator:List:[10,8]
After remove 8:List:[10]