C++ LIST实例

来源:互联网 发布:发现利润区 知乎 编辑:程序博客网 时间:2024/06/04 19:57
#include using namespace std; //(0)定义相关类型typedef struct{int id;char name[20] ;int age;}T_student;// student列表类型定义 typedef list< T_student*, allocator > LSTSTUDENT; typedef list< T_student*, allocator >::iterator ITSTU; LSTSTUDENT stuList;void testAddLIST(){T_student * pStu=NULL;for(int i=0;i<3;i++){ pStu=new T_student(); memset(pStu,0,sizeof(T_student)); pStu->id=i+1; strcpy(pStu->name,"name"); pStu->age=20+i; stuList.push_back(pStu);}}void printAll(){printf("All data is:/n");ITSTU itStu;for (itStu = stuList.begin(); itStu != stuList.end(); itStu++){T_student * pTemp=*itStu;printf("/tid=%d/tname=%s/tage=%d/n",pTemp->id,pTemp->name,pTemp->age);}}void deleteAll(){ITSTU itStu;for (itStu = stuList.begin(); itStu != stuList.end(); itStu++){T_student * pTemp=*itStu;delete pTemp;pTemp=NULL;}stuList.clear();}void deleteOne(int id){ITSTU itStu;for (itStu = stuList.begin(); itStu != stuList.end(); itStu++){T_student * pTemp=*itStu;if(pTemp->id==id){stuList.remove(pTemp);delete pTemp;pTemp=NULL;break;}}}T_student * getOne(int id){ITSTU itStu;for (itStu = stuList.begin(); itStu != stuList.end(); itStu++){T_student * pTemp=*itStu;if(pTemp->id==id){return pTemp;}}return NULL;}void main(){testAddLIST();printAll();deleteOne(1);printAll();deleteAll();printAll();}
原创粉丝点击