14-6-1C++的list

(一)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的数据存取

  1. 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;

}

相关推荐
阿正的梦工坊2 小时前
JavaScript 微任务与宏任务完全指南
开发语言·javascript·ecmascript
知行合一。。。2 小时前
Python--05--面向对象(属性,方法)
android·开发语言·python
自信150413057592 小时前
重生之从0开始学习c++之模板初级
c++·学习
青梅橘子皮2 小时前
C语言---指针的应用以及一些面试题
c语言·开发语言·算法
浅时光_c3 小时前
3 shell脚本编程
linux·开发语言·bash
历程里程碑3 小时前
2. Git版本回退全攻略:轻松掌握代码时光机
大数据·c++·git·elasticsearch·搜索引擎·github·全文检索
极客智造3 小时前
深度解析 C++ 类继承与多态:面向对象编程的核心
c++
Evand J3 小时前
【三维轨迹目标定位,CKF+RTS,MATLAB程序】基于CKF与RTS平滑的三维非线性目标跟踪(距离+方位角+俯仰角)
开发语言·matlab·目标跟踪
今天又在写代码4 小时前
java-v2
java·开发语言
competes5 小时前
慈善基金投资底层逻辑应用 顶层代码低代码配置平台开发结构方式数据存储模块
java·开发语言·数据库·windows·sql