#include<iostream> #include<list> #include<algorithm> using namespace std; //iterator 迭代器:遍历容器(list) void show(int v){ cout<<v<<" "; } int main(){ list<int> lst; //定义链表 lst.push_back(1);//尾添加 lst.push_back(2); lst.push_back(3); lst.push_back(4); lst.push_front(6);// 头添加 list<int>::iterator ite;//定义迭代器 ite = lst.begin(); //返回头节点迭代器 while(ite!=lst.end()){ //end为尾结点迭代器 cout<<*ite<<" "; ite++; } cout<<endl; lst.pop_back();//头删除 lst.pop_front();//尾删除 //增强for循环输出删除后的 for(int val:lst){ cout<<val<<" "; } cout<<endl; cout<<lst.front()<<" "<<lst.back()<<endl; cout<<"链表的长度"<<lst.size()<<endl; //lst.clear();//链表清空 if(lst.empty()){ cout<<"链表为空"<<endl; }else{ cout<<"链表bu为空"<<endl; } ite=++lst.begin(); list<int>::iterator ite2=lst.insert(ite,10);//在指向的节点之前插入,返回插入的节点 cout<<*ite2<<endl; for(int val:lst){ cout<<val<<" "; } cout<<endl; ite = ++lst.begin(); /*list<int>::iterator ite3=lst.erase(ite);*///删除迭代器指向的节点,该迭代器将失效,返回的删除节点的下一个节点的迭代器 ite=lst.erase(ite); cout<<*ite<<endl; /*cout<<*ite3<<endl;*/ for(int val:lst){ cout<<val<<" "; } cout<<endl; cout<<"_______________"<<endl; ::for_each(lst.begin(),lst.end(),&show); cout<<endl; cout<<*lst.begin()<<endl; cout<<*(--lst.end())<<endl; system("pause"); return 0; }
标签:map,12,iterator,迭代,ite,list,lst,push,节点 From: https://www.cnblogs.com/hualuoyumufeng/p/15949611.html