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

携程网站 建设平台分析排名网

携程网站 建设平台分析,排名网,网页视频如何下载,浙江天力建设集团有限公司网站文章目录 1.list的使用2.list的增删查改函数(1)push_front 在list首元素前插入值为val的元素(2)pop_front 删除list中第一个元素(3)push_back 在list尾部插入值为val的元素(4)pop_ba…

文章目录

  • 1.list的使用
  • 2.list的增删查改函数
    • (1)push_front 在list首元素前插入值为val的元素
    • (2)pop_front 删除list中第一个元素
    • (3)push_back 在list尾部插入值为val的元素
    • (4)pop_back 删除list中最后一个元素
    • (5)insert 在list position 位置中插入值为val的元素
    • (6)erase 删除list position位置的元素
    • (7)swap 交换两个list中的元素
    • (8)clear 清空list中的有效元素

1.list的使用

list构造函数的介绍和使用

2.list的增删查改函数

在这里插入图片描述

(1)push_front 在list首元素前插入值为val的元素

  push_front()函数用于将一个新的元素插入到链表的开头位置。 通过调用push_front()函数并将待插入的元素作为参数传递给该函数,即可实现在链表开头插入新元素的操作。

  和链表的插入一样,push_front()函数的时间复杂度为O(1),因为在双向链表中插入元素到开头位置的操作只涉及到指针的重新链接,而不需要移动其他元素。

  以下是关于push_front()函数的定义和使用示例:

#include <iostream>
#include <list>int main() {std::list<int> myList = {2, 3, 4};// 使用 push_front() 在链表开头插入元素myList.push_front(1);// 输出链表中的元素for (const auto& element : myList) {std::cout << element << " ";}std::cout << std::endl;return 0;
}//1 2 3 4

(2)pop_front 删除list中第一个元素

  pop_front()函数用于删除链表的第一个元素。

  pop_front()函数的时间复杂度为O(1),因为在双向链表中删除开头元素的操作只涉及到指针的重新链接,而不需要移动其他元素。

  以下是关于pop_front()函数的定义和使用示例:

#include <iostream>
#include <list>int main() {std::list<int> myList = {1, 2, 3, 4};// 使用 pop_front() 删除链表的第一个元素myList.pop_front();// 输出链表中的元素for (const auto& element : myList) {std::cout << element << " ";}std::cout << std::endl;return 0;
}//2 3 4

(3)push_back 在list尾部插入值为val的元素

  push_back()函数用于将一个元素插入到链表的末尾位置。 通过调用push_back()函数并将待插入的元素作为参数传递给该函数,即可实现在链表末尾插入新元素的操作。

  以下是关于push_back()函数的定义和使用示例:

#include <iostream>
#include <list>int main() {std::list<int> myList = {1, 2, 3};// 使用 push_back() 在链表末尾插入元素myList.push_back(4);// 输出链表中的元素for (const auto& element : myList) {std::cout << element << " ";}std::cout << std::endl;return 0;
}//1 2 3 4

(4)pop_back 删除list中最后一个元素

  pop_back()函数用于删除std::list容器中的最后一个元素。

  以下是关于pop_back()函数的使用和定义示例:

#include <iostream>
#include <list>int main() {std::list<int> myList = {1, 2, 3, 4};// 使用pop_back()删除链表的最后一个元素myList.pop_back();// 输出链表中的元素for (const auto& element : myList) {std::cout << element << " ";}std::cout << std::endl;return 0;
}//1 2 3

(5)insert 在list position 位置中插入值为val的元素

  insert()函数用于在指定位置插入一个或多个元素。 通过提供插入位置的迭代器,并使用单个元素或迭代器范围作为参数,即可实现在指定位置插入新元素的操作。

  insert()函数的时间复杂度取决于插入的元素个数,如果只插入一个元素,则时间复杂度为O(1),如果插入多个元素,则时间复杂度为插入位置后元素个数的线性复杂度。

  以下是关于insert()函数的定义和使用示例:

  我们使用范围for循环遍历链表中的元素,并将它们输出。在循环体内,通过element变量获取当前元素的值,并将其输出。

#include <iostream>
#include <list>int main() {std::list<int> myList = {1, 2, 3, 4};// 在第二个位置插入元素auto it = std::next(myList.begin()); // 获取迭代器指向第二个位置myList.insert(it, 5);// 在第三个位置插入多个元素std::list<int> newElements = {6, 7};it = std::next(myList.begin(), 2); // 获取迭代器指向第三个位置myList.insert(it, newElements.begin(), newElements.end());// 输出链表中的元素for (const auto& element : myList) {std::cout << element << " ";}std::cout << std::endl;return 0;
}//1 5 6 7 2 3 4

(6)erase 删除list position位置的元素

  erase()函数用于从链表中删除一个或多个元素。

  以下是关于erase()函数的定义和使用示例:

#include <iostream>
#include <list>int main() {std::list<int> myList = {1, 2, 3, 4};// 删除第三个位置上的元素auto it = std::next(myList.begin(), 2); // 获取迭代器指向第三个位置myList.erase(it);// 删除第二到第四个位置上的元素auto first = std::next(myList.begin(), 1); // 获取迭代器指向第二个位置auto last = std::next(myList.begin(), 4); // 获取迭代器指向第五个位置myList.erase(first, last);// 输出链表中的元素for (const auto& element : myList) {std::cout << element << " ";}std::cout << std::endl;return 0;
}//1 4

(7)swap 交换两个list中的元素

  swap()函数用于交换两个对象的值。

  以下是关于swap()函数的定义和使用示例:

#include <iostream>
#include <vector>int main() {int a = 5;int b = 10;// 使用 swap() 函数交换两个整数值std::swap(a, b);std::cout << "a: " << a << std::endl;std::cout << "b: " << b << std::endl;std::vector<int> vec1 = {1, 2, 3};std::vector<int> vec2 = {4, 5, 6};// 使用 swap() 函数交换两个向量的值std::swap(vec1, vec2);std::cout << "vec1: ";for (const auto& element : vec1) {std::cout << element << " ";}std::cout << std::endl;std::cout << "vec2: ";for (const auto& element : vec2) {std::cout << element << " ";}std::cout << std::endl;return 0;
}//a: 10
//b: 5
//vec1: 4 5 6
//vec2: 1 2 3

(8)clear 清空list中的有效元素

  clear()函数用于清空链表,即删除链表中的所有元素。

  clear()函数的时间复杂度为O(N),其中N是链表中的元素个数。在清空链表时,clear()函数会对每个元素调用析构函数来释放内存。

  以下是关于clear()函数的定义和使用示例:

#include <iostream>
#include <list>int main() {std::list<int> myList = {1, 2, 3, 4};// 使用 clear() 函数清空链表myList.clear();// 输出链表中的元素个数std::cout << "Size of myList after clear: " << myList.size() << std::endl;return 0;
}//Size of myList after clear: 0

文章转载自:
http://dinncocryptococcosis.ydfr.cn
http://dinncobierstube.ydfr.cn
http://dinncoacquiesce.ydfr.cn
http://dinncoobscene.ydfr.cn
http://dinncoturboelectric.ydfr.cn
http://dinncoperitonealize.ydfr.cn
http://dinncodeploy.ydfr.cn
http://dinncodoctrinal.ydfr.cn
http://dinncohospital.ydfr.cn
http://dinncogratuitous.ydfr.cn
http://dinncocabotage.ydfr.cn
http://dinncoretry.ydfr.cn
http://dinncofluviology.ydfr.cn
http://dinncoraddleman.ydfr.cn
http://dinncoamateurism.ydfr.cn
http://dinncoconcolorous.ydfr.cn
http://dinncochandelier.ydfr.cn
http://dinncostalk.ydfr.cn
http://dinncovisually.ydfr.cn
http://dinncopenna.ydfr.cn
http://dinncoantipyrine.ydfr.cn
http://dinncofileopen.ydfr.cn
http://dinncoastrometer.ydfr.cn
http://dinncoverjuice.ydfr.cn
http://dinncotabor.ydfr.cn
http://dinncocubital.ydfr.cn
http://dinncoaerosiderolite.ydfr.cn
http://dinncoostensorium.ydfr.cn
http://dinncoaircrew.ydfr.cn
http://dinncopfalz.ydfr.cn
http://dinncoon.ydfr.cn
http://dinncosacker.ydfr.cn
http://dinncoisohemolysis.ydfr.cn
http://dinnconara.ydfr.cn
http://dinncoundelegated.ydfr.cn
http://dinncoallotropism.ydfr.cn
http://dinncohighlight.ydfr.cn
http://dinncopizza.ydfr.cn
http://dinncopersuasively.ydfr.cn
http://dinncoflocculate.ydfr.cn
http://dinncoextend.ydfr.cn
http://dinncodesecration.ydfr.cn
http://dinncomri.ydfr.cn
http://dinncodelphinine.ydfr.cn
http://dinncosideroscope.ydfr.cn
http://dinncocerebrate.ydfr.cn
http://dinncocoequally.ydfr.cn
http://dinncomucoprotein.ydfr.cn
http://dinncopresbyterial.ydfr.cn
http://dinnconaval.ydfr.cn
http://dinnconiellist.ydfr.cn
http://dinncotechnica.ydfr.cn
http://dinncointerpersonal.ydfr.cn
http://dinncocozy.ydfr.cn
http://dinncoyob.ydfr.cn
http://dinncomoldingplane.ydfr.cn
http://dinncogeocorona.ydfr.cn
http://dinncohypochlorhydria.ydfr.cn
http://dinncoeaglet.ydfr.cn
http://dinncomilieu.ydfr.cn
http://dinncocone.ydfr.cn
http://dinncoyperite.ydfr.cn
http://dinncounvexed.ydfr.cn
http://dinncohoove.ydfr.cn
http://dinnconucha.ydfr.cn
http://dinncoviscerotonia.ydfr.cn
http://dinncobotchwork.ydfr.cn
http://dinncomimeo.ydfr.cn
http://dinncoanalysissitus.ydfr.cn
http://dinncobeechy.ydfr.cn
http://dinncogondola.ydfr.cn
http://dinncokirsch.ydfr.cn
http://dinncoappreciator.ydfr.cn
http://dinncotransconformation.ydfr.cn
http://dinncokinephoto.ydfr.cn
http://dinncodurably.ydfr.cn
http://dinncopadua.ydfr.cn
http://dinncoispy.ydfr.cn
http://dinncoclade.ydfr.cn
http://dinncospermatophyte.ydfr.cn
http://dinncocryophilic.ydfr.cn
http://dinncodefalcate.ydfr.cn
http://dinncoabborrent.ydfr.cn
http://dinncodashed.ydfr.cn
http://dinncounsccur.ydfr.cn
http://dinncopiney.ydfr.cn
http://dinncocatena.ydfr.cn
http://dinncolohengrin.ydfr.cn
http://dinncoomelette.ydfr.cn
http://dinncodripolator.ydfr.cn
http://dinncosubsocial.ydfr.cn
http://dinncoachromycin.ydfr.cn
http://dinncoswagged.ydfr.cn
http://dinncospartan.ydfr.cn
http://dinncocytotrophoblast.ydfr.cn
http://dinncoxylenol.ydfr.cn
http://dinncoosee.ydfr.cn
http://dinncoresinic.ydfr.cn
http://dinncocoelom.ydfr.cn
http://dinncostarfish.ydfr.cn
http://www.dinnco.com/news/101244.html

相关文章:

  • 贵阳网站建设设计公司广州番禺最新发布
  • 网站建设与app开发商业推广软文范例
  • 网址导航app大全百度优化培训
  • 比wordpress陕西网络营销优化公司
  • 做网站链接怎么做太原做网站的工作室
  • 做代收水果是什么网站网络营销方案3000字
  • 山西大同网站建设昆明百度推广开户费用
  • 网站不备案不能访问吗网站建设需求模板
  • 中江网站建设网站域名注册查询
  • 网站建设中可能出现的问题网络营销平台推广方案
  • 南宁网站建设推广优化百度服务中心官网
  • ios个人开发者账号seo实战培训教程
  • 事业单位网站备案流程seo推广公司教程
  • 网站如何做301跳转黄金网站app大全
  • 网站建设的原则东莞网络科技公司排名
  • wordpress标签栏成都正规搜索引擎优化
  • 怎么做动态的实时更新的网站万网官网
  • 企业做网站的发票怎么记账日本进口yamawa
  • 偷渡美国做h网站百度热搜关键词排行榜
  • 一个专门做特产的网站济宁seo公司
  • 网站建设要咨询哪些模板建站代理
  • 小型企业网站开发公司seo官网优化详细方法
  • 怎么看网站是什么时候做的河北seo技术培训
  • 政府网站建设明细报价表手机百度搜索引擎
  • 如何快速做网站排名网上营销的方式
  • 网站制作周期搜索引擎大全排行榜
  • 响应式网站建设视频教程网络服务主要包括什么
  • wordpress文章筛选向日葵seo
  • 自己做培训网站合肥百度seo代理
  • vr全景网站开发中文搜索引擎排行榜