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;

}

相关推荐
weixin_472339462 小时前
高效处理大体积Excel文件的Java技术方案解析
java·开发语言·excel
枯萎穿心攻击3 小时前
响应式编程入门教程第二节:构建 ObservableProperty<T> — 封装 ReactiveProperty 的高级用法
开发语言·unity·c#·游戏引擎
Eiceblue4 小时前
【免费.NET方案】CSV到PDF与DataTable的快速转换
开发语言·pdf·c#·.net
tan180°5 小时前
MySQL表的操作(3)
linux·数据库·c++·vscode·后端·mysql
m0_555762905 小时前
Matlab 频谱分析 (Spectral Analysis)
开发语言·matlab
浪裡遊6 小时前
React Hooks全面解析:从基础到高级的实用指南
开发语言·前端·javascript·react.js·node.js·ecmascript·php
彭祥.6 小时前
Jetson边缘计算主板:Ubuntu 环境配置 CUDA 与 cudNN 推理环境 + OpenCV 与 C++ 进行目标分类
c++·opencv·分类
lzb_kkk6 小时前
【C++】C++四种类型转换操作符详解
开发语言·c++·windows·1024程序员节
好开心啊没烦恼7 小时前
Python 数据分析:numpy,说人话,说说数组维度。听故事学知识点怎么这么容易?
开发语言·人工智能·python·数据挖掘·数据分析·numpy
简佐义的博客7 小时前
破解非模式物种GO/KEGG注释难题
开发语言·数据库·后端·oracle·golang