(一)list容器的基本概念
list容器簡介:
1.list是一個雙向鏈表容器,可高效地進行插入刪除元素
2.list不可以隨機存取元素,所以不支持at.(pos)函數與[ ]操作符
(二)list容器頭部和尾部的操作
list對象的默認構造形式:list<T>lst
list<int> lstInt;
list<float>lstFloat;
list塊頭尾的添加移除操作
1.list.push_front(elem);//在容器開頭插入一個元素
2.lst.push_back(elem);//在容器尾部加入元素
#include <iostream>
#include <list>
using namespace std;
int main()
{
?? list<int>lst;
?? lst.push_back(10);
?? lst.push_front(0);
?? list <int>::iterator it;
?? for(it=lst.begin() ;it!=lst.end() ;it++)
?? {
????????? cout<<*it<<" ";
?? }
?? cout<<endl;
?? return 0;
}
3.list.pop_back0;//刪除容器中最后一個元素
#include <iostream>
#include <list>
using namespace std;
int main()
{
?? ?list<int>lst;
?? ?lst.push_back(10);
?? ?lst.push_front(0);
?? ?list <int>::iterator it;
?? ?lst.pop_back();
?? ?for(it=lst.begin() ;it!=lst.end() ;it++)
?? ?{
?? ??? ?cout<<*it<<" ";
?? ?}
?? ?cout<<endl;
?? ?return 0;?
}
4.list.pop_front();//從容器開頭移除第一個元素
#include <iostream>
#include <list>
using namespace std;
int main()
{
?? ?list<int>lst;
?? ?lst.push_back(10);
?? ?lst.push_front(0);
?? ?list <int>::iterator it;
?? ?lst.pop_front();
?? ?for(it=lst.begin() ;it!=lst.end() ;it++)
?? ?{
?? ??? ?cout<<*it<<" ";
?? ?}
?? ?cout<<endl;
?? ?return 0;?
}
list的數據存取
- list.front();//返回第一個元素
#include <iostream>
#include <list>
using namespace std;
int main()
{
?? ?list<int>lst;
?? ?lst.push_back(10);
?? ?lst.push_front(0);
?? ?list <int>::iterator it;
?? ?int x=lst.front();
?? ?cout<<"front="<<x<<endl;
?? ?return 0;?
}
2.list.back();//返回最后一個元素
#include <iostream>
#include <list>
using namespace std;
int main()
{
? list<int>lst;
? lst.push_back(10);
? lst.push_front(0);
? list <int>::iterator it;
? int y=lst.back();
? cout<<"back="<<y<<endl;
? return 0;
}
#include <iostream>
#include <list>
using namespace std;
int main()
{
?? ?list<int>lst;
?? ?lst.push_back(10);
?? ?lst.push_front(0);
?? ?list <int>::iterator it;
?? ?int y=lst.back();
?? ?cout<<"back="<<y<<endl;
?? ?return 0;?
}
數據的修改
#include <iostream>
#include <list>
using namespace std;
int main()
{
?? ?list<int>lst;
?? ?lst.push_back(10);
?? ?lst.push_front(0);
?? ?list <int>::iterator it;
? ? lst.front()=100;
?? ?lst.back() =200;
?? ?for(it=lst.begin();it!=lst.end() ;it++)
?? ?{
?? ??? ?cout<<*it<<" ";
?? ?}
?? ?cout<<endl;
?? ?return 0;?
}
(三)list與迭代器
list容器的迭代器是“雙向迭代器”:雙向迭代器從兩個方向讀寫容器。除了提供前向迭代器的全部操作之外,雙向迭代器還提供前置和后置的自減運算
rend | begin | ...... | rbegin | end |
正向1.list.begin();//返容器中第一個元素的迭代器
正向2.list.end();//返回容器中最后一個元素之后的迭代器
#include <iostream>
#include <list>
using namespace std;
int main()
{
?? ?list<int>lst;
?? ?lst.push_back(1);?
?? ?lst.push_back(2);
?? ?lst.push_back(3);
?? ?lst.push_back(4) ;
?? ?list <int>::iterator it;
? ?
?? ?for(it=lst.begin();it!=lst. end() ;it++)
?? ?{
?? ??? ?cout<<*it<<" ";
?? ?}
?? ?cout<<endl;
?? ?return 0;?
}
反向3.list.rbegin();//返回容器中倒數第一個元素的迭代器
反向4.list.rend();//返回容器中倒數最后一個元素的后面的迭代器
#include <iostream>
#include <list>
using namespace std;
int main()
{
?? ?list<int>lst;
?? ?lst.push_back(1);?
?? ?lst.push_back(2);
?? ?lst.push_back(3);
?? ?lst.push_back(4) ;
?? ?list <int>::reverse_iterator it;
? ?
?? ?for(it=lst.rbegin();it!=lst.rend() ;it++)
?? ?{
?? ??? ?cout<<*it<<" ";
?? ?}
?? ?cout<<endl;
?? ?return 0;?
}