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

这么做网站免费外链发布平台

这么做网站,免费外链发布平台,世界排名第几,开源建站软件list使用 构造函数insert && 迭代器push_back && pop_back && push_front && pop_fronterasesort && find && reverse list的底层结构就是 带头双向循环链表 构造函数 // 默认构造 list<int> lt; cout << "l…

list使用

  • 构造函数
  • insert && 迭代器
  • push_back && pop_back && push_front && pop_front
  • erase
  • sort && find && reverse

list的底层结构就是 带头双向循环链表

构造函数

// 默认构造
list<int> lt;
cout << "lt->";
for (auto it : lt)
{cout << it << " ";
}
cout << endl;// 用n个val来进行初始化
list<int> lt1(10);
cout << "lt1->";
for (auto it : lt1)
{cout << it << " ";
}
cout << endl;list<int> lt2(10, 1);
cout << "lt2->";
for (auto it : lt2)
{cout << it << " ";
}
cout << endl;// 迭代器区间进行初始化
list<int> lt3(lt2.begin(), lt2.end());
cout << "lt3->";
for (auto it : lt3)
{cout << it << " ";
}
cout << endl;// 拷贝构造
list<int>lt4 = lt3;
cout << "lt4->";
for (auto it : lt4)
{cout << it << " ";
}
cout << endl;

运行结果:

lt->
lt1->0 0 0 0 0 0 0 0 0 0
lt2->1 1 1 1 1 1 1 1 1 1
lt3->1 1 1 1 1 1 1 1 1 1
lt4->1 1 1 1 1 1 1 1 1 1

insert && 迭代器



看来list并没有 重载 + 运算符
但是重载了++


list<int> lt2(10, 1);
cout << "lt2->";
for (auto it : lt2)
{
cout << it << " ";
}
cout << endl;cout << "插入->";
lt2.insert(lt2.begin()++, 10);
for (auto it : lt2)
{cout << it << " ";
}
cout << endl;

运行结果:

lt2->1 1 1 1 1 1 1 1 1 1
插入->10 1 1 1 1 1 1 1 1 1 1

如果, 我们想要在 第五个位置进行插入该怎么办?

list<int> lt2(10, 1);
cout << "lt2->";
for (auto it : lt2)
{
cout << it << " ";
}
cout << endl;// 先找到下标为5的迭代器
auto it = lt2.begin();
for (int i = 0; i < 5; i++)
{++it;
}list<int>::iterator c = lt2.insert(it, 50);
cout << "插入->";
for (auto it : lt2)
{cout << it << " ";
}
cout << endl;cout << "insert返回值的解引用->" << * c << endl;

运行结果:

lt2->1 1 1 1 1 1 1 1 1 1
插入->1 1 1 1 1 50 1 1 1 1 1
insert返回值的解引用->50

list迭代器遍历时,while循环的判断条件只能是!=,不能是<

list<int> lt2(10, 1);
cout << "lt2->";
for (auto it : lt2)
{
cout << it << " ";
}
cout << endl;// 迭代器遍历
list<int>::iterator it = lt2.begin();
cout << "迭代器遍历->";
while (it != lt2.end())
{cout << *it << " ";++it;
}
cout << endl;

运行结果:

lt2->1 1 1 1 1 1 1 1 1 1
迭代器遍历->1 1 1 1 1 1 1 1 1 1

push_back && pop_back && push_front && pop_front

	list<int> lt2(5, 1);cout << "lt2->";for (auto it : lt2){cout << it << " ";}cout << endl;lt2.pop_back();lt2.pop_back();lt2.pop_back();lt2.pop_back();cout << "尾删->";for (auto it : lt2){cout << it << " ";}cout << endl;lt2.push_back(13);lt2.push_back(30);lt2.push_back(45);lt2.push_back(6);lt2.push_back(60);lt2.push_back(5);lt2.push_back(7);cout << "尾插->";for (auto it : lt2){cout << it << " ";}cout << endl;lt2.pop_front();lt2.pop_front();lt2.pop_front();lt2.pop_front();lt2.pop_front();cout << "头删->";for (auto it : lt2){cout << it << " ";}cout << endl;lt2.push_front(25);lt2.push_front(55);lt2.push_front(9);lt2.push_front(6);lt2.push_front(20);cout << "头插->";for (auto it : lt2){cout << it << " ";}cout << endl;

运行结果:

lt2->1 1 1 1 1
尾删->1
尾插->1 13 30 45 6 60 5 7
头删->60 5 7
头插->20 6 9 55 25 60 5 7

erase

// 删除lt2中的所有奇数
auto it = lt2.begin();
while (it != lt2.end())
{if ((*it) % 2 == 1){// erase返回删除后的下一个节点的地址 -- 更新了itit = lt2.erase(it); }
else{// 不是奇数, 那就++it++;}
}
cout << "删除所有奇数->";
for (auto it : lt2)
{
cout << it << " ";
}
cout << endl;

运行结果:

lt2->1 1 1 1 1 1 13 30 45 6
删除所有奇数->30 6

但是我们需要注意: erase的形参是不能再使用的, 切记!切记!

sort && find && reverse

当我们调用 算法库中的sort:


🗨️这是怎么一回事?

  • 其实, 迭代器可以按照性质(有底层结构决定)来进行划分:
    单向迭代器 – – 可以进行 ++
    比如: forward_list/ unordered_map/ unordered_set
    双向迭代器 – – 可以进行 ++/ –
    比如: list/ map/ set
    随机迭代器 – – 可以进行+/ - / ++/ –
    比如: vector/ deque/ string


但是std::sort的迭代器使用的是 随机迭代器类型 ⇒ 故 list不能使用算法库中的sort算法

但list类中自带sort算法👇👇👇

//sort(lt2.begin(), lt2.end());
lt2.sort();
cout << "升序->";
for (auto e : lt2)
{cout << e << " ";
}
cout << endl;lt2.sort(greater<int>());
cout << "降序->";
for (auto e : lt2)
{cout << e << " ";
}
cout << endl;

运行结果:

升序->1 5 6 7 13 30 45 60
降序->60 45 30 13 7 6 5 1

温馨提示:

list自带的排序很慢的, 小数据量(<10万)的可以用用,
但大数据量还不如 将数据拷贝到vector中, vector排好序之后, 再将结果拷贝回去快
使用好 对应的容器, 对效率来说很重要的

find函数list并没有, 但是可以用算法库中的

list函数自带reverse函数, 但其实还不如用库里的


学须反己。若徒责人,只见得人不是,不见自己非。若能反己,方见自己有许多未尽处,奚暇责人? — — 王阳明
译: 有位朋友经常因为生气而指责别人。王阳明告诫他说:“学习应该反身自省。如果只去指责别人,就只能看到别人的错误,就不会看到自己的缺点。若能返身自省,才能看到自己有许多不足之处,哪还有时间去指责别人?
舜的弟弟叫象,象屡次想害死舜,但舜还是照样疼他。王阳明说,舜之所以能感化象的傲慢,最主要的就是舜不去看象的不是。如果舜坚决要去纠正象的奸恶,只会看到象的不是,而象又是一个傲慢的人,肯定不会认错,舜又岂能感化他?” 这位朋友听了这番话,甚感惭愧。

http://www.hkea.cn/news/490560/

相关文章:

  • 站长网站查询深圳百度关键字优化
  • 用net语言做网站平台好不好企业培训师资格证报考2022
  • 成都定制网站设竞价推广遇到恶意点击怎么办
  • 制作视频网站建设友链交易网
  • 做外贸是不是要有网站腾讯企点app下载安装
  • 网站开发快递文件国外网站怎么推广
  • 网站和搜索引擎站长论坛
  • 做违法网站会怎样外贸独立站怎么建站
  • 云主机建网站教程深圳全网推互联科技有限公司
  • 做网站赚50万谷歌搜索引擎363入口
  • 台州网站设计外包网页制作公司排名
  • 网站建设投标文件范本亚马逊提升关键词排名的方法
  • 学做网站需要多长时间免费推广平台排行
  • wordpress运行php 404360优化大师下载
  • seo排名网站 优帮云线上推广的三种方式
  • 平凉哪有做网站的百度推广登录入口官网网
  • 娄底网站优化自建网站平台有哪些
  • 做网站需要多少兆空间wix网站制作
  • 哪些网站教做生物实验今日新闻联播
  • 铜川市住房和城乡建设局网站信息流广告哪个平台好
  • 太原市建设交易中心网站首页百度手机助手app安卓版官方下载
  • 昆山网站建设网站建设郑州网络推广哪个好
  • 瑜伽网站设计国外推广网站
  • 什么网站做国外批发百度推广自己怎么做
  • 网站管理工具百度推广可以自己开户吗
  • 三水网站制作中山做网站推广公司
  • ysl网站设计论文郑州seo地址
  • 做食品的网站设计要注意片多多可以免费看电视剧吗
  • 网站排名推广自己怎么做长沙seo代理商
  • 手机网站改版公司加盟关键词优化排名查询