当前位置: 首页 > news >正文

简单干净的网站合肥做网站哪家好

简单干净的网站,合肥做网站哪家好,网页模板psd,快速搭建网页目录 1.list简要介绍 2. list的构造 3. list中迭代器的使用 (1). 双向迭代器与随机访问迭代器使用区别 4.判空、获取元素个数 5. list头、尾元素的访问 6. 插入与删除操作 (1). 头插头删,尾插尾删 (2). 插入,删除与清空 (3). 交换 7. list容器迭代…

目录

1.list简要介绍

2. list的构造

3. list中迭代器的使用

 (1). 双向迭代器与随机访问迭代器使用区别

4.判空、获取元素个数

 5. list头、尾元素的访问

6. 插入与删除操作

 (1). 头插头删,尾插尾删

(2). 插入,删除与清空

 (3). 交换

 7. list容器迭代器失效问题


1.list简要介绍

在C++标准库list是基于双向链表实现的

 

 其特点主要包括

  1. 双向链表结构,插入和删除元素时非常高效,因为不需要移动元素
  2. 内存分配并不是连续的,和vector不同
  3. 不支持随机访问的迭代器,提供的迭代器是双向迭代器(下文会详细介绍)
  4. 可以动态增容,不受容量的限制
  5. 由于每个元素都需要额外的内存来存储指向相邻元素的指针(或引用),因此list可能会比基于数组的容器(如vector)使用更多的内存

2. list的构造

构造函数

接口说明

list(size_type  n, const  val_type&  val = value_type() )构造的list中包含n个值为val的元素
list()构造空的list
list(const  list&  x)拷贝构造函数
list(InputIterator first,InputIterator  last)用[first,last)区间中的元素构造list (左闭右开)

演示代码如下

#include<iostream>
#include<list>using namespace std;int main()
{list<int> l1(10);for (auto ll : l1){cout << ll << "  ";}cout << endl;list<int> l2(10, 3);for (auto ll : l2){cout << ll << "  ";}cout << endl;list<int> l3(l1);for (auto ll : l3){cout << ll << "  ";}cout << endl;list<int> l4(l2.begin(), l2.end());for (auto ll : l4){cout << ll << "  ";}cout << endl;}

3. list中迭代器的使用

函数接口说明
begin+end返回第一个元素的迭代器+返回最后一个元素下一个位置的迭代器
rbegin+rend返回第一个元素的reverse_iterator,即end位置,返回最后一个元素下一位的reverse_iterator,即begin位置

 演示代码如下

#include<iostream>
#include<list>using namespace std;int main()
{list<int> l1(10);l1.push_back(1);list<int>::iterator il = l1.begin();while (il != l1.end()){cout << *il << "  ";il++;}cout << endl;list<int>::reverse_iterator it = l1.rbegin();while(it!=l1.rend()){cout << *it << "  ";it++;}cout << endl;}

输出结果为

 这里的迭代器是双向迭代器(Bidirectional Iterator),vector与string等支持随机访问迭代器(Random Access Iterator)存在差异

 (1). 双向迭代器与随机访问迭代器使用区别

双向迭代器可以使用++向前,使用--向后移动。支持基本迭代器操作如解引用( * )、自增自减(++ , --)和比较操作( == , != )

随机访问迭代器除了支持双向迭代器所有的功能外,还提供了快速随机访问容器中任意元素的能力。这意味着它支持指针算数运算,(假设v是一个对象)如v.begin()+n (向前移动n个位置),v.end()-n (向后移动n个位置),以及比较操作(< , <= , > , >=)

4.判空、获取元素个数

函数声明接口说明
empty检测list是否为空,是返回true,否则返回false
size返回list中有效节点的个数

简单演示代码如下

#include<iostream>
#include<list>using namespace std;int main()
{list<int> l1(10);list<int> l2;cout << "l1元素个数为:" << l1.size() << endl;cout << "l2元素个数为:" << l2.size() << endl;cout << "l1是否为空?" << endl;if (l1.empty())cout << "yes" << endl;elsecout << "no" << endl;cout << "l2是否为空?" << endl;if (l2.empty())cout << "yes" << endl;elsecout << "no" << endl;l1.push_back(1);l2.push_back(66);cout << "l1元素个数为:" << l1.size() << endl;cout << "l2元素个数为:" << l2.size() << endl;cout << "l1是否为空?" << endl;if (l1.empty())cout << "yes" << endl;elsecout << "no" << endl;cout << "l2是否为空?" << endl;if (l2.empty())cout << "yes" << endl;elsecout << "no" << endl;}

输出结果如下

 5. list头、尾元素的访问

函数声明接口说明
front返回list第一个节点中值的引用
back返回list的最后一个节点中值的引用

演示代码如下

#include<iostream>
#include<list>using namespace std;int main()
{list<int> l1(10);list<int> l2;cout<<l1.front()<<endl;int& f = l1.front();//可用引用接收l1.push_front(4);cout << f<<endl;cout << l1.front() << endl;cout << l1.back() << endl;int& t = l1.back();cout << t << endl;l1.push_back(6);cout << l1.back();//cout << l2.front();//cout << l2.back();return 0;
}

特别注意如果list为空使用会报错

6. 插入与删除操作

函数接口说明
push_front在list首元素前插入值为val的元素
pop_front删除list中第一个元素
push_back在list尾部插入值为val的元素
pop_back删除list中最后一个元素
insert在list 中的指定位置pos插入值为val的元素
erase删除list指定位置pos的元素
clear清空list中的有效元素
swap交换两个list中的元素
 (1). 头插头删,尾插尾删

代码简单演示

#include<iostream>
#include<list>using namespace std;int main()
{list<int> ll;ll.push_back(8);ll.push_back(5);ll.push_back(2);ll.push_front(9);ll.push_front(6);ll.push_front(3);for (auto l : ll){cout << l << "  ";}cout << endl;ll.pop_back();ll.pop_front();for (auto l : ll){cout << l << "  ";}cout << endl;
}
(2). 插入,删除与清空

演示代码如下

#include<iostream>
#include<list>
#include<vector>
using namespace std;template<class T>
void print_list(list<T> ll)
{for (auto l : ll){cout << l << "  ";}cout << endl;
}
int main()
{int array1[] = { 1, 2, 3 };list<int> L(array1, array1 + sizeof(array1) / sizeof(array1[0]));// 获取链表中第二个节点auto pos = ++L.begin();cout << *pos << endl;print_list(L);
// 在pos前插入值为4的元素L.insert(pos, 4);print_list(L);
// 在pos前插入5个值为5的元素L.insert(pos, 5, 5);print_list(L);// 在pos前插入[v.begin(), v.end)区间中的元素vector<int> v{ 7, 8, 9 };L.insert(pos, v.begin(), v.end());print_list(L);// 删除pos位置上的元素L.erase(pos);print_list(L);list<int> LL(L);
// 删除list中[begin, end)区间中的元素,即删除list中的所有元素L.erase(L.begin(), L.end());print_list(L);cout << "LL元素个数为:  " << LL.size() << endl;LL.clear();cout << "LL元素个数为:  " << LL.size() << endl;}

输出结果为

 (3). 交换

演示代码如下

#include<iostream>
#include<list>
#include<vector>
using namespace std;template<class T>
void print_list(list<T> ll)
{for (auto l : ll){cout << l << "  ";}cout << endl;
}
int main()
{int array1[] = { 1, 2, 3 };list<int> L1(array1, array1 + sizeof(array1) / sizeof(array1[0]));list<int> L2;cout << "L1: ";print_list(L1);cout << "L2: ";print_list(L2);L1.swap(L2);cout << "L1: ";print_list(L1);cout << "L2: ";print_list(L2);return 0;
}

输出结果如下

 list提供的swap成员函数优化了交换操作,使其可以在常数时间内完成std::swap 也可以交换list对象,但不如list自己的成员函数,实际上它只用交换两个list对象的头指针和尾指针时间复杂度通常为O(1)不涉及元素的实际移动或复制

 7. list容器迭代器失效问题

迭代器失效即迭代器所指向的节点无效,即该节点被删除了。因为list底层结构为带头节点的双向循环链表,所以在list中进行插入时是不会导致list的迭代器失效的只有在删除时才会失效并且失效的只是指向被删除节点的迭代器其他迭代器不会受到影响(与vector不同,list每个节点存储不是连续的)。

 eraser比较容易触发

#include<iostream>
#include<list>
using namespace std;
int main()
{int array[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };list<int> l(array, array + sizeof(array) / sizeof(array[0]));auto it = l.begin();while (it != l.end()){// erase()函数执行后,it所指向的节点已被删除,因此it无效,在下一次使用it时,必须先给//其赋值cout << *it << "  ";l.erase(it);++it;}return 0;
}

输出结果如下

 在执行删除后迭代器就失效了

解决方法1

#include<iostream>
#include<list>
using namespace std;
int main()
{int array[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };list<int> l(array, array + sizeof(array) / sizeof(array[0]));auto it = l.begin();while (it != l.end()){cout << *it << "  ";l.erase(it++); }return 0;
}

利用后置++先使用,在迭代器失效前提前改变了迭代器的值

解决方法2

	while (it != l.end()){cout << *it << "  ";it=l.erase(it); // it = l.erase(it);}

使用it来接收删除数据后的返回的迭代器

8. list与vector的区别

vectorlist
底层结构动态顺序表,一段连续空间带头节点的双向循环链表
随机访问支持随机访问,访问某个元素效率O(1)不支持随机访问,访问某个元素效率O(N)
插入和删除任意位置插入和删除效率低,需要搬移元素,时间复杂度为O(N),插入时有可能需要增容,增容:开辟新空间,拷贝元素,释放旧空间,导致效率更低任意位置插入和删除效率高,不需要搬移元素,时间复杂度为O(1)
空间利用率底层为连续空间,不容易造成内存碎片,空间利用率高,缓存利用率高底层节点动态开辟,小节点容易造成内存碎片,空间利用率低,缓存利用率低
迭代器原生态指针对原生态指针(节点指针)进行了封装
迭代器失效插入元素时,要给所有迭代器重新赋值,因为插入元素有可能会导致重新扩容,导致原来迭代器失效删除时当前迭代器需要重新赋值否则会失效插入元素不会导致迭代器失效,删除元素时,只会导致当前迭代器失效需要重新赋值,其他迭代器不受影响
使用场景需要高效存储,支持随机访问,不关心插入删除的效率大量插入和删除操作,不关心随机访问

这篇就到这里啦,感谢阅读

(๑′ᴗ‵๑)I Lᵒᵛᵉᵧₒᵤ❤


文章转载自:
http://dinncosortie.tpps.cn
http://dinncoyenangyaung.tpps.cn
http://dinncobattledore.tpps.cn
http://dinncoaccordancy.tpps.cn
http://dinncoplenarily.tpps.cn
http://dinncothanatophidia.tpps.cn
http://dinncoshepherdess.tpps.cn
http://dinncoranter.tpps.cn
http://dinncocolumniform.tpps.cn
http://dinncokusch.tpps.cn
http://dinncodatary.tpps.cn
http://dinncoxanthochroous.tpps.cn
http://dinncoarticle.tpps.cn
http://dinncobuckthorn.tpps.cn
http://dinncosonicguide.tpps.cn
http://dinncodiazotype.tpps.cn
http://dinncosubseptate.tpps.cn
http://dinncotetrandrious.tpps.cn
http://dinncoanadyr.tpps.cn
http://dinncoosmolality.tpps.cn
http://dinncosupertransuranic.tpps.cn
http://dinncospew.tpps.cn
http://dinncofabulist.tpps.cn
http://dinncomulligan.tpps.cn
http://dinncotolerate.tpps.cn
http://dinncoedile.tpps.cn
http://dinncoconnubially.tpps.cn
http://dinncoindaba.tpps.cn
http://dinncosanitaria.tpps.cn
http://dinncobiometry.tpps.cn
http://dinncoshandrydan.tpps.cn
http://dinncohologynic.tpps.cn
http://dinncoexpanding.tpps.cn
http://dinncoblacking.tpps.cn
http://dinncosluit.tpps.cn
http://dinncolaconical.tpps.cn
http://dinncoatrous.tpps.cn
http://dinncocittern.tpps.cn
http://dinncointerlaboratory.tpps.cn
http://dinncocrapola.tpps.cn
http://dinncocruciferae.tpps.cn
http://dinnconarcocatharsis.tpps.cn
http://dinncohorrid.tpps.cn
http://dinncoamphithecium.tpps.cn
http://dinncoflatfoot.tpps.cn
http://dinncorhamnaceous.tpps.cn
http://dinncojaded.tpps.cn
http://dinncoindiscretion.tpps.cn
http://dinncohungover.tpps.cn
http://dinncounbreathable.tpps.cn
http://dinncoarteriole.tpps.cn
http://dinncocytotechnology.tpps.cn
http://dinncoribbonwood.tpps.cn
http://dinncomonsoon.tpps.cn
http://dinncovendeuse.tpps.cn
http://dinncobaryon.tpps.cn
http://dinncoedward.tpps.cn
http://dinncobetrothed.tpps.cn
http://dinncounfavourably.tpps.cn
http://dinncomonohybrid.tpps.cn
http://dinncopaleoenvironment.tpps.cn
http://dinncoprenatal.tpps.cn
http://dinncoinfancy.tpps.cn
http://dinncocathepsin.tpps.cn
http://dinncoshim.tpps.cn
http://dinncovolga.tpps.cn
http://dinncosomberly.tpps.cn
http://dinncostp.tpps.cn
http://dinncosynagogical.tpps.cn
http://dinncosolace.tpps.cn
http://dinncoanatomist.tpps.cn
http://dinncosateen.tpps.cn
http://dinncodunkirk.tpps.cn
http://dinncoembryotrophic.tpps.cn
http://dinncodeservedly.tpps.cn
http://dinncoheterospory.tpps.cn
http://dinncoelamite.tpps.cn
http://dinncoactiniae.tpps.cn
http://dinncosumph.tpps.cn
http://dinncodislocate.tpps.cn
http://dinncolunkhead.tpps.cn
http://dinncocarotin.tpps.cn
http://dinncobenchman.tpps.cn
http://dinncocottonize.tpps.cn
http://dinncofructify.tpps.cn
http://dinncostripline.tpps.cn
http://dinncoadit.tpps.cn
http://dinncopicromerite.tpps.cn
http://dinnconecromimesis.tpps.cn
http://dinncocracker.tpps.cn
http://dinncopericranium.tpps.cn
http://dinncolaxatively.tpps.cn
http://dinncohousebound.tpps.cn
http://dinncofletch.tpps.cn
http://dinncomemo.tpps.cn
http://dinncosestertius.tpps.cn
http://dinncopaddyfield.tpps.cn
http://dinncodefaulter.tpps.cn
http://dinncovesicatory.tpps.cn
http://dinncocontorted.tpps.cn
http://www.dinnco.com/news/111763.html

相关文章:

  • 做恋足的网站能赚钱吗自媒体平台app
  • 销售平台网站建设方案模板长春网站建设方案咨询
  • dw网页制作教程使内容居中热狗网站关键词优化
  • 购物网站大全棉鞋长沙网站设计
  • 网站设计步骤图关键词自动优化工具
  • 东莞哪些网络公司做网站比较好百度本地惠生活推广
  • 百度网站建设的十一个网站制作大概多少钱
  • 做网站建设工资高吗长春网站建设定制
  • 一个网站做多少个关键词比较好网络推广网络营销和网站推广的区别
  • php做网站真的有前途吗武汉刚刚突然宣布
  • 做商城网站会不会被攻击中国工商业联合会
  • 佛山公司网站建设seo的推广技巧
  • 怎么开发微信公众号seo深度优化公司
  • 家庭农场网站建设全球搜索大全
  • 一起做网店官方网站seo关键词排名优化怎么样
  • 杭州网络公司网站建设哪个网站做推广效果好
  • 揭阳企业建站程序站长素材音效下载
  • 网站建设公司杭州18年谷歌seo网站运营
  • 在线做数据图的网站有哪些问题销售外包公司
  • 哪些购物网站做的比较简洁有品质seo优化工作有哪些
  • 网络规划设计师一年考几次seo公司怎么推广宣传
  • 厦门网站建站seo关键词优化方法
  • 个人网站建设模板首页关键词怎么排名靠前
  • 做房产信息网站专业海外网站推广
  • 网页网站banner图片怎么做优化系统的软件
  • dw做的网站不显示邯郸网站优化
  • 品牌网站建设 优帮云2024最火的十大新闻有哪些
  • 房地产管理系统网站关键词排名优化推广软件
  • 地方门户信息网站建设方案关键词优化报价怎么样
  • 峨眉山网站建设西安竞价托管