网站百度建设,yfcms企业网站建设,烟台商城网站建设,网站建设友汇list容器是一个双向链表容器#xff0c;可以高效地进行插入删除元素#xff0c;但是不能随机存取元素#xff08;不支持at()和[]操作符#xff09;。一、list容器的对象构造方法list对象采用模板类的默认构造形式例如listT lst#xff1b;#includeiostream可以高效地进行插入删除元素但是不能随机存取元素不支持at()和[]操作符。一、list容器的对象构造方法list对象采用模板类的默认构造形式例如listT lst#includeiostream
#includelist
using namespace std;
int main()
{int arr[]{0,1,2,3,4};listint lstInt;listfloat lstFloat;liststring listString;listint::iterator t1;listint::iterator t2;lstInt.assign(arr,arr5);lstInt.push_back(5);//在容器尾部插入元素lstInt.push_back(5);//在容器尾部删除元素lstInt.pop_back();lstInt.push_front(0);//在容器头部插入元素lstInt.push_front(0);lstInt.pop_front();//在容器头部删除元素t1lstInt.begin(); t2lstInt.end(); // 正确写法 for(;t1!t2;t1){cout*t1;}coutendl;// 错误写法
// for(;t1t2;t1)
// {
// cout*t1;
// }
// coutendl;//输出0012345 return 0;
}list对象的带参构造方式listT lst(beg,end该构造函数将区间[beg,end)中的元素拷贝给本身。beg,end是数组元素的地址。listT list(n,elem该构造函数将n个elem拷贝给本身。listT lst1(lst2拷贝构造函数#includeiostream
#includelist
using namespace std;
int main()
{int arr[]{0,1,2,3,4};listint::iterator t;//1、list对象带参数构造
//正确写法 listint lst1(arr,arr5); //建立一个存放int的list容器,初始为01234 listint lst2(lst1.begin(),lst1.end()); //建立一个存放int的list容器初始为01234 listint lst3(3,100); listint lst4(lst1); for(tlst1.begin();t!lst1.end();t){cout*t ;}coutendl;for(tlst2.begin();t!lst2.end();t){cout*t ;}coutendl;for(tlst3.begin();t!lst3.end();t){cout*t ;}coutendl;for(tlst4.begin();t!lst4.end();t){cout*t ;}coutendl;//输出
//0 1 2 3 4
//0 1 2 3 4
//100 100 100
//0 1 2 3 4 return 0;
} 二、list与迭代器list容器的迭代器是双向迭代器。list.begin();返回容器第一个元素的迭代器。list.end();返回容器最后一个元素之后的迭代器。list.rbegin();返回容器倒数第一个元素的迭代器。list.rend();返回容器倒数最后一个元素后面的迭代器。#includeiostream
#includelist
using namespace std;
int main()
{int arr[]{0,1,2,3,4};listint lstInt;listint::iterator t1;listint::iterator t2;lstInt.assign(arr,arr5);t1lstInt.begin(); t2lstInt.end(); // 正确写法 for(;t1!t2;t1){cout*t1;}coutendl;// 错误写法
// for(;t1t2;t1)
// {
// cout*t1;
// }
// coutendl;//输出01234return 0;
}三、list容器的赋值1、list.assign(beg,end) 将区间[beg,end)中的元素拷贝给本身。2、list.assign(n,elem)将n个elem拷贝给本身。3、list operator(const list vec)重载等号操作符。4、list.swap(vec)将vec与本身的元素交换。#includeiostream
#includelist
using namespace std;
int main()
{int arr[]{0,1,2,3,4};listint::iterator t;listint lst1; listint lst2; listint lst3; lst1.assign(arr,arr5);lst2.assign(3,100);lst3lst1;lst2.swap(lst1);for(tlst1.begin();t!lst1.end();t){cout*t ;}coutendl;for(tlst2.begin();t!lst2.end();t){cout*t ;}coutendl;for(tlst2.begin();t!lst2.end();t){cout*t ;}coutendl;
//输出
//100 100 100
//0 1 2 3 4
//0 1 2 3 4
return ;
} 四、list容器的大小list.size()返回容器中元素的个数list.empty()判断容器是否为空list.resize(num)重新指定容器长度若比之前的长度长超出部分填充默认值若比之前的长度短删除超出部分元素。list.resize(num,elem)重新指定容器长度若比之前的长度长超出部分填充指定值若比之前的长度短删除超出部分元素。五、list容器元素的插入list.insert(poselem在pos位置插入一个elem元素返回新元素的位置迭代器类型list.insert(posn, elem在pos位置插入n个elem元素无返回值list.insert(posbeg, end在pos位置插入[begend区间的数据无返回值。六、list容器的删除1、list.clear();移除容器的所有数据2、list.erase(beg,end);删除[beg,end区间的数据返回下一个数据的位置。3、list.erase(pos);删除pos位置的元素返回下一个数据的位置。4、list.remove(elem);删除容器里所有值为elem的元素。#includeiostream
#includelist
using namespace std;
int main()
{int arr[]{0,1,2,3,4};listint lstInt;listint::iterator t1;listint::iterator t2;listint::iterator t3;lstInt.assign(arr,arr5);t1lstInt.begin(); for(;t1!lstInt.end();t1){cout*t1;}coutendl;//01234lstInt.clear();lstInt.push_front(5);lstInt.push_front(6);lstInt.push_front(7);lstInt.push_front(8);for(t1lstInt.begin();t1!lstInt.end();t1){cout*t1;}coutendl;//8765t2lstInt.begin();t3lstInt.begin();t3;t3;lstInt.erase(t2,t3);for(t1lstInt.begin();t1!lstInt.end();t1){cout*t1;}coutendl;//85lstInt.push_front(5);lstInt.push_front(6);lstInt.push_front(7);lstInt.push_front(8);lstInt.remove(5);for(t1lstInt.begin();t1!lstInt.end();t1){cout*t1;}coutendl;//8768//输出
//01234
//8765
//85
//8768return 0;
}七、其他lst.reverse();反转列表#includeiostream
#includelist
using namespace std;
int main()
{int arr[]{0,1,2,3,4};listint lstInt;listint::iterator t1;listint::iterator t2;lstInt.assign(arr,arr5);lstInt.reverse();t1lstInt.begin(); t2lstInt.end(); for(;t1!t2;t1){cout*t1;}coutendl;
// 输出43210 return 0;
}2、删除结点导致迭代器失效#includeiostream
#includelist
using namespace std;
int main()
{int arr[]{0,1,2,3,4,4,4,4,4,4,4,5,5,6,6};listint lstInt;listint::iterator t1;listint::iterator t2;lstInt.assign(arr,arr15);t1lstInt.begin(); t2lstInt.end(); //因为list容器使用不连续分配的内存并且它的erase方法会返回下一个有效的迭代器所有遍历删除结点可以有以下方式
//方法1 for(;t1!t2;){if(*t14){t1lstInt.erase(t1);}else{t1;}}//方法2for(;t1!t2;t1){if(*t14){lstInt.erase(t1);}}t1lstInt.begin(); t2lstInt.end(); for(;t1!t2;t1){cout*t1 ;}coutendl;
//输出0 1 2 3 5 5 6 6 return 0;
}