#include <bits/stdc++.h>
using namespace std;
struct node{
int Number;
bool operator==(node b)const{
return this->Number==b.Number;
}
bool operator!=(node b)const{
return this->Number!=b.Number;
}
bool operator>=(node b)const{
return this->Number>=b.Number;
}
bool operator<=(node b)const{
return this->Number<=b.Number;
}
bool operator>(node b)const{
return this->Number>b.Number;
}
bool operator<(node b)const{
return this->Number<b.Number;
}
};
list<node> List;
list<node>::iterator it;
void node_delete()
{
list<node>::iterator iter;
for(iter=List.begin();iter!=List.end();){
std::cout<< "delete before iter->number:"<<iter->Number <<std::endl;
iter=List.erase(iter);
std::cout<< "delete after iter->number:"<< iter->Number <<std::endl;
}
}
inline int node_insert(node _add,int insert_place){
int now=0;
if(insert_place<0) return -1;
list<node>::iterator iter=List.begin();
while(iter!=List.end()){
if(now==insert_place){
iter = List.insert(iter,_add);//insert a factor after thr factor of "*iter",return the palce of _add in list;
printf("\n>>insert one in List,it's number is %d\n",iter->Number);
return 0;
}
now++;
++iter;
}
return 0;
}
void look_for(node _find){
printf("\ncome to look_for function\n");
list<node>::iterator iter;
iter=find(List.begin(),List.end(),_find);
if(iter!=List.end()) printf("find it,it.Number is %d\n",(*iter).Number);
else printf("not find it\n");//if not find,return List.end();
return;
}
int main()
{
//初始化
srand(time(0));
node add_temp;
//添加
for(int i=1;i<=10;i++){
add_temp.Number=rand()%100;
List.push_back(add_temp);
}
//遍历
printf("\nThe List is:\n");
for(it=List.begin();it!=List.end();it++) printf("%d ",it->Number);
printf("\n#end\n");
//插入
add_temp.Number = 80;
node_insert(add_temp,3);
//遍历
printf("\nThe List is(after node_insert(add_temp,3)):\n");
for(it=List.begin();it!=List.end();it++) printf("%d ",it->Number);
printf("\n#end\n");
//查找
node find_temp;
find_temp.Number=80;
look_for(find_temp);
//排序
List.sort();
//遍历
printf("\nThe List is(after Sort(List)):\n");
for(it=List.begin();it!=List.end();it++) printf("%d ",it->Number);
printf("\n#end\n");
//删除末尾
cout<<endl<<"Tist.size:"<<List.size()<<endl;
List.pop_back();
//遍历
printf("The List is(after List.pop_back()):\n");
for(it=List.begin();it!=List.end();it++) printf("%d ",it->Number);
printf("\n#end");
cout<<endl<<"Tist.size:"<<List.size()<<endl;
//删除所有
list<node> temp;
temp=List;
List.clear();
cout<<endl<<"Tist.size:"<<List.size()<<endl;
List=temp;
node_delete();
cout<<endl<<"Tist.size:"<<List.size()<<endl;
return 0;
}