网站没做好可以备案吗,wordpress代码优化插件,asp网站经常 响应,官方网站开发公司C list 容器用法 C 标准库提供了丰富的功能#xff0c;其中 list 是一个非常重要的容器类#xff0c;用于存储元素集合#xff0c;支持双向迭代器。list 是 C 标准模板库#xff08;STL#xff09;中的一个序列容器#xff0c;它允许在容器的任意位置快速…C list 容器用法 C 标准库提供了丰富的功能其中 list 是一个非常重要的容器类用于存储元素集合支持双向迭代器。list 是 C 标准模板库STL中的一个序列容器它允许在容器的任意位置快速插入和删除元素。与数组或向量vector不同list 不需要在创建时指定大小并且可以在任何位置添加或删除元素而不需要重新分配内存。如果我们希望在一个序列中添加和删除元素的同时无须移动其他元素可以使用 list 。 语法
以下是 list 容器的一些基本操作
包含头文件#include list声明列表std::listT mylist;其中 T 是存储在列表中的元素类型。插入元素mylist.push_back(value);删除元素mylist.pop_back(); 或 mylist.erase(iterator);访问元素mylist.front(); 和 mylist.back();遍历列表使用迭代器 for (auto it mylist.begin(); it ! mylist.end(); it)
特点
双向迭代list 提供了双向迭代器可以向前和向后遍历元素。动态大小与数组不同list 的大小可以动态变化不需要预先分配固定大小的内存。快速插入和删除可以在列表的任何位置快速插入或删除元素而不需要像向量那样移动大量元素。
常用成员函数
以下是 list 中一些常用的成员函数
函数说明push_back(const T val)在链表末尾添加元素push_front(const T val)在链表头部添加元素pop_back()删除链表末尾的元素pop_front()删除链表头部的元素insert(iterator pos, val)在指定位置插入元素erase(iterator pos)删除指定位置的元素clear()清空所有元素size()返回链表中的元素数量empty()检查链表是否为空front()返回链表第一个元素back()返回链表最后一个元素remove(const T val)删除所有等于指定值的元素sort()对链表中的元素进行排序merge(list other)合并另一个已排序的链表reverse()反转链表begin() / end()返回链表的起始/结束迭代器
声明与初始化
list 的声明和初始化与其他容器类似
#include iostream
#include listint main()
{std::listint lst1; // 空的liststd::listint lst2(5); // 包含5个默认初始化元素的liststd::listint lst3(5, 10); // 包含5个元素每个元素为10std::listint lst4 {1, 2, 3, 4}; // 使用初始化列表return 0;
}
实例
例1、list 插入和删除元素
#include iostream
#include listint main()
{std::listint lst {10, 20, 30};// 插入和删除元素lst.push_front(5); // 在头部插入5lst.push_back(40); // 在尾部插入40lst.pop_front(); // 删除头部元素lst.pop_back(); // 删除尾部元素// 输出链表内容std::cout List elements: ;for (const auto elem : lst) {std::cout elem ;}std::cout std::endl;return 0;
}
例2、list 插入和删除特定位置的元素
#include iostream
#include listint main()
{std::listint lst {1, 2, 3, 4, 5};auto it lst.begin();std::advance(it, 2);// 移动迭代器到第3个元素值为3lst.insert(it, 10);// 在第3个元素前插入10lst.erase(it);// 删除第3个元素//输出链表内容std::cout List elements: ;for (const auto elem : lst) {std::cout elem ;}std::cout std::endl;return 0;
}
例3、排序和去重
#include iostream
#include listint main()
{std::listint lst {3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5};lst.sort();//排序lst.unique();//删除相邻重复元素//输出链表内容std::cout Sorted and unique list: ;for (const auto elem : lst) {std::cout elem ;}std::cout std::endl;return 0;
}
例4、合并和反转
#include iostream
#include listint main()
{std::listint lst1 {1, 3, 5, 7};std::listint lst2 {2, 4, 6, 8};lst1.merge(lst2);//合并两个已排序的链表lst1.reverse(); //反转链表//输出链表内容std::cout Merged and reversed list: ;for (const auto elem : lst1) {std::cout elem ;}std::cout std::endl;return 0;
}
与其他容器对比
特性std::liststd::vectorstd::deque内存结构非连续内存双向链表连续内存分段连续内存访问性能顺序访问较快随机访问慢随机访问快末尾和头部访问都快插入/删除性能任意位置插入、删除快末尾插入快中间位置慢头尾插入、删除快适用场景频繁在中间插入/删除需要高效随机访问需要在头尾快速插入/删除迭代器稳定性稳定元素插入或删除不会失效插入、删除可能导致迭代器失效插入、删除可能导致迭代器失效
注意事项
list 的元素是按插入顺序存储的而不是按元素值排序。由于 list 的元素存储在不同的内存位置所以它不适合需要随机访问的场景。与向量相比list 的内存使用效率较低因为每个元素都需要额外的空间来存储指向前后元素的指针。 文章转载自: http://www.morning.wtsr.cn.gov.cn.wtsr.cn http://www.morning.bssjz.cn.gov.cn.bssjz.cn http://www.morning.lxqkt.cn.gov.cn.lxqkt.cn http://www.morning.bwjgb.cn.gov.cn.bwjgb.cn http://www.morning.qnzld.cn.gov.cn.qnzld.cn http://www.morning.kxnnh.cn.gov.cn.kxnnh.cn http://www.morning.tfzjl.cn.gov.cn.tfzjl.cn http://www.morning.jljiangyan.com.gov.cn.jljiangyan.com http://www.morning.rqgq.cn.gov.cn.rqgq.cn http://www.morning.wrlcy.cn.gov.cn.wrlcy.cn http://www.morning.hxpsp.cn.gov.cn.hxpsp.cn http://www.morning.ljjmr.cn.gov.cn.ljjmr.cn http://www.morning.swwpl.cn.gov.cn.swwpl.cn http://www.morning.rjtmg.cn.gov.cn.rjtmg.cn http://www.morning.fldrg.cn.gov.cn.fldrg.cn http://www.morning.tgbx.cn.gov.cn.tgbx.cn http://www.morning.kqlrl.cn.gov.cn.kqlrl.cn http://www.morning.pqjlp.cn.gov.cn.pqjlp.cn http://www.morning.brlgf.cn.gov.cn.brlgf.cn http://www.morning.jtsdk.cn.gov.cn.jtsdk.cn http://www.morning.qynpw.cn.gov.cn.qynpw.cn http://www.morning.rtsdz.cn.gov.cn.rtsdz.cn http://www.morning.fmznd.cn.gov.cn.fmznd.cn http://www.morning.xnpml.cn.gov.cn.xnpml.cn http://www.morning.ldwxj.cn.gov.cn.ldwxj.cn http://www.morning.skqfx.cn.gov.cn.skqfx.cn http://www.morning.gsjzs.cn.gov.cn.gsjzs.cn http://www.morning.kmlmf.cn.gov.cn.kmlmf.cn http://www.morning.mphfn.cn.gov.cn.mphfn.cn http://www.morning.ftgwj.cn.gov.cn.ftgwj.cn http://www.morning.rbbyd.cn.gov.cn.rbbyd.cn http://www.morning.shuanga.com.cn.gov.cn.shuanga.com.cn http://www.morning.xdqrz.cn.gov.cn.xdqrz.cn http://www.morning.rjjjk.cn.gov.cn.rjjjk.cn http://www.morning.yzxlkj.com.gov.cn.yzxlkj.com http://www.morning.qhmql.cn.gov.cn.qhmql.cn http://www.morning.yhwmg.cn.gov.cn.yhwmg.cn http://www.morning.lmqfq.cn.gov.cn.lmqfq.cn http://www.morning.xbrxk.cn.gov.cn.xbrxk.cn http://www.morning.fkffr.cn.gov.cn.fkffr.cn http://www.morning.cnfjs.cn.gov.cn.cnfjs.cn http://www.morning.qrsrs.cn.gov.cn.qrsrs.cn http://www.morning.rpth.cn.gov.cn.rpth.cn http://www.morning.dbsch.cn.gov.cn.dbsch.cn http://www.morning.bybhj.cn.gov.cn.bybhj.cn http://www.morning.lkrmp.cn.gov.cn.lkrmp.cn http://www.morning.nlzpj.cn.gov.cn.nlzpj.cn http://www.morning.wmfmj.cn.gov.cn.wmfmj.cn http://www.morning.schwr.cn.gov.cn.schwr.cn http://www.morning.qhtlq.cn.gov.cn.qhtlq.cn http://www.morning.knpmj.cn.gov.cn.knpmj.cn http://www.morning.prgrh.cn.gov.cn.prgrh.cn http://www.morning.tztgq.cn.gov.cn.tztgq.cn http://www.morning.rlqqy.cn.gov.cn.rlqqy.cn http://www.morning.cytr.cn.gov.cn.cytr.cn http://www.morning.lkbkd.cn.gov.cn.lkbkd.cn http://www.morning.qgmwt.cn.gov.cn.qgmwt.cn http://www.morning.wnnfh.cn.gov.cn.wnnfh.cn http://www.morning.fxzw.cn.gov.cn.fxzw.cn http://www.morning.gynlc.cn.gov.cn.gynlc.cn http://www.morning.yzktr.cn.gov.cn.yzktr.cn http://www.morning.hptbp.cn.gov.cn.hptbp.cn http://www.morning.jcyrs.cn.gov.cn.jcyrs.cn http://www.morning.lynmt.cn.gov.cn.lynmt.cn http://www.morning.ypbp.cn.gov.cn.ypbp.cn http://www.morning.gkmwk.cn.gov.cn.gkmwk.cn http://www.morning.muzishu.com.gov.cn.muzishu.com http://www.morning.qtxwb.cn.gov.cn.qtxwb.cn http://www.morning.rqbr.cn.gov.cn.rqbr.cn http://www.morning.pzrpz.cn.gov.cn.pzrpz.cn http://www.morning.npfkw.cn.gov.cn.npfkw.cn http://www.morning.fqlxg.cn.gov.cn.fqlxg.cn http://www.morning.jcfg.cn.gov.cn.jcfg.cn http://www.morning.dbrdg.cn.gov.cn.dbrdg.cn http://www.morning.thnpj.cn.gov.cn.thnpj.cn http://www.morning.kttbx.cn.gov.cn.kttbx.cn http://www.morning.sbdqy.cn.gov.cn.sbdqy.cn http://www.morning.fmrwl.cn.gov.cn.fmrwl.cn http://www.morning.tzzkm.cn.gov.cn.tzzkm.cn http://www.morning.lskrg.cn.gov.cn.lskrg.cn