博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
c++STL中的List容器详解
阅读量:3967 次
发布时间:2019-05-24

本文共 7553 字,大约阅读时间需要 25 分钟。

c++STL中的List容器详解

一、提要

List 是一个双向链表容器,List容器能够在任何地方快速的删除添加元素。

List 容器的接口与其他容器的接口相似,List容器不能随机数据存取元素(如:用下标或者用at.()方法都是不可以的),数据存取我有必要提一下,因为我以前也搞不懂,我今天问了老师才知道,数据存就是把数据存进去,修改它的值,取就是把数据取出来,这里存的什么值,使用迭代器时可以进行++操作,但不可以进行+3,+4操作,使用时需包含头文件 和deque 容器相同的操作时我只是复习一下的知识,就简单的提一下,不会去深讲,有需要的可以去看我之前发的deque容器详解

二、List 容器的构造函数

//List 容器默认构造函数和带参数的构造函数void demo1(void) {	//List默认构造函数	list
listInt; cout<<"listInt默认构造函数的元素个数:" << listInt.size() << endl; //List带参数的构造函数 list
listIntA(10,666); cout << "listIntA带参数的构造函数的元素个数:" << listIntA.size() << endl; vector
vectInt; vectInt.capacity(); //List 容器没有capacity() 容量函数 不存在预先分配空间 //List 使用迭代器访问元素 for (list
::iterator it = listIntA.begin(); it != listIntA.end(); it++) { cout <<"listIntA带参构造函数的元素:"<< (*it) << endl; }}

运行结果:

在这里插入图片描述

三、List 容器的头尾添加删除操作和数据的存取

//List 容器的头尾添加删除操作和数据的存取void demo2(void) {	list
listInt; //在尾部追加元素 listInt.push_back(1); listInt.push_back(2); listInt.push_back(3); listInt.push_back(4); listInt.push_back(5); //在头部删除元素 listInt.pop_front(); listInt.pop_front(); //在尾部删除元素 listInt.pop_back(); listInt.pop_back(); //头部添加元素 listInt.push_front(11); listInt.push_front(12); //使用迭代器遍历打印 for (list
::iterator it = listInt.begin(); it != listInt.end(); it++) { cout << "listInt添加删除过后:" << (*it) << endl; } cout << "------------数据存取------------" << endl; //数据的存取 listInt.back() = 14;//listInt的最后一个元素赋值为14 listInt.front() = 16;//listInt的前面第一个元素赋值为16 for (list
::iterator it = listInt.begin(); it != listInt.end(); it++) { cout << "listInt数据存取过后:" << (*it) << endl; }}

运行结果:

在这里插入图片描述

四、List 容器与迭代器

//List 容器与迭代器void demo3(void) {	list
listInt; //这里与deque相似可以看我以前发的deque容器详解,这里只是回顾一下以前的知识. listInt.push_back(1); listInt.push_back(2); listInt.push_back(3); listInt.push_back(4); listInt.push_back(5); //使用普通的迭代器遍历打印 for (list
::iterator it = listInt.begin(); it != listInt.end(); it++) { cout << "listInt的元素:" << (*it) << endl; } cout << " -----------------常量迭代器---------------------" << endl; //使用常量迭代器遍历打印 for (list
::const_iterator cit = listInt.cbegin();cit != listInt.cend(); cit++) { cout << "listInt的元素:" << (*cit) << endl; } cout << " -----------------逆转迭代器---------------------" << endl; //使用逆转迭代器遍历打印 for (list
::reverse_iterator rit = listInt.rbegin(); rit != listInt.rend(); rit++) { cout << "listInt的元素:" << (*rit) << endl; }}

运行结果:

在这里插入图片描述

五、List 容器的赋值和大小

//List 容器的赋值和大小void demo4(void) {	list
listInt,listIntA,listIntB,listIntC; listInt.push_back(1); listInt.push_back(2); listInt.push_back(3); listInt.push_back(4); listInt.push_back(5); //第一种 //指定区间,listInt.begin()到listInt.end()的元素,赋值给listIntB //注意:包括listInt.begin()但不包括listInt.end()指向的元素(左闭右开), listIntB.assign(listInt.begin(), listInt.end()); for (list
::iterator it = listIntB.begin(); it != listIntB.end(); it++) { //打印结果:1,2,3,4,5 listIntB.end() 不是指向的5,是指向的5后面的元素 cout << "listIntB的元素:" << (*it) << endl; } //也可以用前置++ 前置-- listIntB.assign(++listInt.begin(), --listInt.end()); for (list
::iterator it = listIntB.begin(); it != listIntB.end(); it++) { //打印结果: 2,3,4 cout << "listIntB使用前置--++赋值的元素:" << (*it) << endl; } //第二种, //指定元素个数和元素值 listIntC.assign(4,666); for (list
::iterator it = listIntC.begin(); it != listIntC.end(); it++) { //打印结果: 666,666,666,666, cout << "listIntC使用是指定元素个数和元素值赋值:" << (*it) << endl; } //第三种 使用赋值构造函数 listIntA = listIntC; for (list
::iterator it = listIntA.begin(); it != listIntA.end(); it++) { //打印结果: 666,666,666,666, cout << "listIntA使用赋值构造函数构造listIntC的元素:" << (*it) << endl; } cout << "---------------list容器的大小--------------" << endl; //这里与deque相似可以看我以前发的deque容器详解,这里只是回顾一下以前的知识. list
listIntD; listIntD.push_back(1); listIntD.push_back(2); listIntD.push_back(3); if (!listIntD.empty()) {//empty int size = listIntD.size();//size=3 listIntD.resize(2); //resize函数是重新定义元素个数//1,2 listIntD.resize(5);//1,2,0,0,0 listIntD.resize(7, 22);//1,2,0,0,0,22,22 } for (list
::iterator it = listIntD.begin(); it != listIntD.end(); it++) { //打印结果://1,2,0,0,0,22,22 cout << "listIntD的元素:" << (*it) << endl; }}

运行结果:

在这里插入图片描述

六、list 容器的插入和删除,还有反序排列

//list 容器的插入和删除,还有反序排列void demo5(void) {		list
listInt; list
listIntA(4, 333); listInt.push_back(1); listInt.push_back(2); listInt.push_back(3); listInt.push_back(4); listInt.push_back(5); cout << "-----------------------元素的插入----------------" << endl; //这里与deque相似可以看我以前发的deque容器详解,这里只是回顾一下以前的知识. //list容器的插入 //第一个参数常常是需要插入的迭代器位置 cout << " 第一种 " << endl; //第一种: 指定区间插入 注意:左闭右开 listInt.insert(listInt.begin(), listIntA.begin(), listIntA.end()); for (list
::iterator it = listInt.begin(); it != listInt.end(); it++) { cout << "listInt的元素:" << (*it) << endl; } cout << " 第二种 " << endl; //第二种:指定元素个数和元素值 在2的位置插入2个666 listInt.insert(++listInt.begin(),2,666); for (list
::iterator it = listInt.begin(); it != listInt.end(); it++) { cout << "listInt的元素:" << (*it) << endl; } cout << " 第三种 " << endl; //第三种:指定元素值 在最后的位置插入一个6 listInt.insert(listInt.end(), 6); for (list
::iterator it = listInt.begin(); it != listInt.end(); it++) { cout << "listInt的元素:" << (*it) << endl; } cout << "--------------元素的删除------------------" << endl; list
listIntB; listIntB.push_back(1); listIntB.push_back(2); listIntB.push_back(3); listIntB.push_back(4); listIntB.push_back(5); listIntB.push_back(6); listIntB.push_back(7); cout << " 第一种 " << endl; //这里与deque容器有些不一样 //list容器 迭代器只能++或--不能执行+3,+4操作 list
::iterator begin = listIntB.begin(); list
::iterator end = listIntB.end(); --end; --end; ++begin; ++begin; //第一种: 指定区间删除 注意:左闭右开 //begin迭代器指向容器的第三个元素end指向容器的倒数第二个元素 //begin到end的元素删除 注意:左闭右开 listIntB.erase(begin,end);//erase()函数返回值是下一个位置的迭代器 for (list
::iterator it = listIntB.begin(); it != listIntB.end(); it++) { //打印结果: 1,2 ,6,7, cout << "listIntB的元素:" << (*it) << endl; } cout << " 第二种" << endl; //第二种: 指定位置删除 //把容器的第一元素删除 listIntB.erase(listIntB.begin()); for (list
::iterator it = listIntB.begin(); it != listIntB.end(); it++) { //打印结果: 2 ,6,7, cout << "listIntB的元素:" << (*it) << endl; } cout << " 第三种 " << endl; listIntB.push_back(4); listIntB.push_back(4); listIntB.push_back(4); listIntB.push_back(4); //第三种:删除指定元素 //会把元素值相同的全部删掉 listIntB.remove(4);//rmemove()方法 for (list
::iterator it = listIntB.begin(); it != listIntB.end(); it++) { //打印结果:还是 2 ,6,7, cout << "listIntB的元素:" << (*it) << endl; } cout << " 第四种 " << endl; //第四种:迭代器遍历删除 //迭代器遍历删除6这个元素 for (list
::iterator it = listIntB.begin(); it != listIntB.end();) { if (*it == 6) { //erase()函数返回值是下一个位置的迭代器,这时it已经指向了一个位置; it = listIntB.erase(it); } else { cout << "listIntB的元素:" << (*it) << endl; //erase()函数返回值是下一个位置的迭代器只有不经过上面的if() it++;//it++如果放在上面不行,越界了 } } cout << "--------------反序排列---------------" << endl; listIntB.push_back(1); listIntB.push_back(2); listIntB.push_back(3); listIntB.push_back(4); //reverse() 函数可以逆转链表如1,2,3,4,5,用reverse()函数之后就是5,4,3,2,1, listIntB.reverse(); for (list
::iterator it = listIntB.begin(); it != listIntB.end(); it++) { cout << "listIntB的元素:" << (*it) << endl; }}

运行结果:

在这里插入图片描述
七、结尾

deque容器没有的还有忘写了的,全都在这代码的注释里了。

转载地址:http://tfyki.baihongyu.com/

你可能感兴趣的文章
ubntu 开发服务进程
查看>>
linux 常用命令以及技巧
查看>>
记录1年免费亚马逊AWS云服务器申请方法过程及使用技巧
查看>>
golang文章
查看>>
linux的system () 函数详解
查看>>
在shell脚本的第一行中,必须写#!/bin/bash
查看>>
一句话##错误 'ASP 0116' 丢失脚本关闭分隔符
查看>>
文件上传漏洞之.htaccess
查看>>
常见网络安全设备默认口令
查看>>
VirtualBox虚拟机网络配置
查看>>
oracle vm virtualbox虚拟机下,CentOS7系统网络配置
查看>>
解决Linux CentOS中cp -f 复制强制覆盖的命令无效的方法
查看>>
wdcpv3升级到v3.2后,多PHP版本共存的安装方法
查看>>
PHP统计当前网站的访问人数,访问信息,被多少次访问。
查看>>
Windows10远程报错CredSSP加密oracle修正
查看>>
Windows server 2016 设置多用户登陆
查看>>
偶然发现的面包屑
查看>>
CentOS 7 下挂载NTFS文件系统磁盘并设置开机自动挂载
查看>>
非插件实现Typecho语法高亮
查看>>
windows 下 netsh 实现 端口映射(端口转发)
查看>>