C++总结笔记(九)常见容器--List

#include<iostream>
#include<list>
#include<numeric>
#include <algorithm>
using namespace std;

void showList()
{
 list<int> iList;
 iList.push_front(1);
 iList.push_front(2);
 iList.push_back(4);
 iList.push_back(3);
 
 list<int>::iterator i;

 cout<<"Display from the front:"<<endl;
 for(i = iList.begin();i != iList.end();i++)
 {
  cout<<*i<<" "<<endl;
 }

 list<int>::reverse_iterator j;
 cout<<"Display from the front:"<<endl;
 for(j = iList.rbegin();j != iList.rend();j++)
 {
  cout<<*j<<" "<<endl;
 }
}

void addList()
{
 list<int> iList;
 iList.push_front(1);
 iList.push_front(2);
 iList.push_back(4);
 iList.push_back(3);

 int result = accumulate(iList.begin(),iList.end(),0);
 cout<<"The result is:"<<result<<endl;
}

void put_list(list<int>lists,char* name)
{
 list<int>::iterator i;

 cout<<"The "<<name<<"will display:";

 for(i=lists.begin();i!=lists.end();i++)
 {
  cout<<*i<<" ";
 }
 cout<<endl;
}

void testList()
{
 list<int> list1;
 list<int> list2(5,6);
 list<int> list3(list2.begin(),--list2.end());

 put_list(list1,"list1");
 put_list(list2,"list2");
 put_list(list3,"list3");
}

void insert()
{
 list<int> list1(2,3);
 list1.insert(++list1.begin(),1,4);
 put_list(list1,"list1");
}

void delList()
{
 list<int> list1;
 list1.push_front(1);
 list1.push_front(2);
 list1.push_front(3);
 list1.push_front(4);
 list1.push_front(5);
 list1.push_front(6);
 list1.push_front(7);
 list1.push_front(8);
 put_list(list1,"list1");

 list1.pop_front();
 list1.pop_back();
 put_list(list1,"list1");

 list1.erase(list1.begin(),--list1.end());
 put_list(list1,"list1");
}

int main()
{
 //showList();
 //addList();
 //testList();
 //insert();
 delList();
 return 0;
}

posted on 2013-06-03 06:10  Kimi_jun  阅读(309)  评论(0编辑  收藏  举报

导航