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;

}

相关推荐
OOJO9 分钟前
c++---list介绍
c语言·开发语言·数据结构·c++·算法·list
笨笨饿2 小时前
29_Z变换在工程中的实际意义
c语言·开发语言·人工智能·单片机·mcu·算法·机器人
艾为电子2 小时前
【技术帖】让接口不再短命:艾为 C-Shielding™ Type-C智能水汽防护技术解析
c语言·开发语言
会编程的土豆3 小时前
【数据结构与算法】动态规划
数据结构·c++·算法·leetcode·代理模式
棉花骑士3 小时前
【AI Agent】面向 Java 工程师的Claude Code Harness 学习指南
java·开发语言
IGAn CTOU3 小时前
PHP使用Redis实战实录2:Redis扩展方法和PHP连接Redis的多种方案
开发语言·redis·php
环黄金线HHJX.3 小时前
TSE框架配置与部署详解
开发语言·python
Vfw3VsDKo3 小时前
Maui 实践:Go 接口以类型之名,给 runtime 传递方法参数
开发语言·后端·golang
Pyeako4 小时前
PyQt5 + PaddleOCR实战:打造桌面级实时文字识别工具
开发语言·人工智能·python·qt·paddleocr·pyqt5
6Hzlia4 小时前
【Hot 100 刷题计划】 LeetCode 78. 子集 | C++ 回溯算法题解
c++·算法·leetcode