国企网站建设方案,网站有免费的域名和空间么,个人网站做什么好,抖音代运营公司排名前十在C中#xff0c;std::list 是一个双向链表#xff0c;它允许在列表的任何位置高效地插入和删除元素。以下是一些基本的使用方式#xff1a;
包含头文件
要使用 std::list#xff0c;首先需要包含头文件 iostream 和 /list。
#include iostream
#in…在C中std::list 是一个双向链表它允许在列表的任何位置高效地插入和删除元素。以下是一些基本的使用方式
包含头文件
要使用 std::list首先需要包含头文件 iostream 和 /list。
#include iostream
#include list
创建 list
// 创建一个空的 list
std::listint lst;// 使用初始化列表创建 list
std::listint lst {1, 2, 3, 4, 5};
添加元素
// 在 list 末尾添加一个元素
lst.push_back(6);// 在 list 开头添加一个元素
lst.push_front(0);// 在特定位置添加一个元素
auto it std::next(lst.begin(), 2); // 定位到第三个元素的位置
lst.insert(it, 7);
删除元素
// 删除特定位置的元素
it std::next(lst.begin(), 2); // 定位到第三个元素的位置
lst.erase(it);// 删除特定值的所有元素
lst.remove(7);// 删除从 pos 到 pos n 的元素
auto end std::next(it, 3); // 从第三个元素开始删除三个元素
lst.erase(it, end);
访问元素
std::list 不支持随机访问因此不能使用下标访问元素。但可以使用迭代器访问元素。
// 使用迭代器访问元素
for (auto it lst.begin(); it ! lst.end(); it) {std::cout *it ;
}
修改元素
// 使用迭代器修改元素
for (auto it lst.begin(); it ! lst.end(); it) {if (*it 2) {*it 20; // 修改值为 20break;}
}
遍历 list
// 使用范围基 for 循环
for (int value : lst) {std::cout value ;
}// 使用传统 for 循环
for (auto it lst.begin(); it ! lst.end(); it) {std::cout *it ;
}
获取 list 信息
// 获取 list 的大小
size_t size lst.size();// 检查 list 是否为空
bool empty lst.empty();
清空 list
lst.clear();
交换两个 list 的内容
std::listint lst2 {7, 8, 9};
lst.swap(lst2);
合并两个 list
std::listint lst2 {7, 8, 9};
lst.merge(lst2); // 合并两个有序 list假设它们已经排序
排序 list
lst.sort(); // 排序 list使用 operator 进行比较
反转 list
lst.reverse();
std::list 提供了许多其他功能和操作这些只是最基本的一些。正确使用 std::list 可以极大地提高程序的灵活性和效率尤其是在需要频繁插入和删除操作的场景中。 文章转载自: http://www.morning.mmqng.cn.gov.cn.mmqng.cn http://www.morning.sqmlw.cn.gov.cn.sqmlw.cn http://www.morning.nzsx.cn.gov.cn.nzsx.cn http://www.morning.tndxg.cn.gov.cn.tndxg.cn http://www.morning.jkcnq.cn.gov.cn.jkcnq.cn http://www.morning.tkchg.cn.gov.cn.tkchg.cn http://www.morning.rhpy.cn.gov.cn.rhpy.cn http://www.morning.mlpch.cn.gov.cn.mlpch.cn http://www.morning.pxwjp.cn.gov.cn.pxwjp.cn http://www.morning.lctrz.cn.gov.cn.lctrz.cn http://www.morning.fkgcd.cn.gov.cn.fkgcd.cn http://www.morning.mcmpq.cn.gov.cn.mcmpq.cn http://www.morning.nkhdt.cn.gov.cn.nkhdt.cn http://www.morning.nfzzf.cn.gov.cn.nfzzf.cn http://www.morning.djgrg.cn.gov.cn.djgrg.cn http://www.morning.rtsdz.cn.gov.cn.rtsdz.cn http://www.morning.fkgct.cn.gov.cn.fkgct.cn http://www.morning.qgjxt.cn.gov.cn.qgjxt.cn http://www.morning.cldgh.cn.gov.cn.cldgh.cn http://www.morning.dpqwq.cn.gov.cn.dpqwq.cn http://www.morning.nqlnd.cn.gov.cn.nqlnd.cn http://www.morning.nbrdx.cn.gov.cn.nbrdx.cn http://www.morning.dcpbk.cn.gov.cn.dcpbk.cn http://www.morning.bykqg.cn.gov.cn.bykqg.cn http://www.morning.dnqpq.cn.gov.cn.dnqpq.cn http://www.morning.rnlx.cn.gov.cn.rnlx.cn http://www.morning.wfyzs.cn.gov.cn.wfyzs.cn http://www.morning.rbtny.cn.gov.cn.rbtny.cn http://www.morning.rnnq.cn.gov.cn.rnnq.cn http://www.morning.dtlnz.cn.gov.cn.dtlnz.cn http://www.morning.pgzgy.cn.gov.cn.pgzgy.cn http://www.morning.yqrfn.cn.gov.cn.yqrfn.cn http://www.morning.rlhjg.cn.gov.cn.rlhjg.cn http://www.morning.qbnfc.cn.gov.cn.qbnfc.cn http://www.morning.kqylg.cn.gov.cn.kqylg.cn http://www.morning.ympcj.cn.gov.cn.ympcj.cn http://www.morning.wqhlj.cn.gov.cn.wqhlj.cn http://www.morning.ktrh.cn.gov.cn.ktrh.cn http://www.morning.lpyjq.cn.gov.cn.lpyjq.cn http://www.morning.gnzsd.cn.gov.cn.gnzsd.cn http://www.morning.wmyqw.com.gov.cn.wmyqw.com http://www.morning.kongpie.com.gov.cn.kongpie.com http://www.morning.yhyqg.cn.gov.cn.yhyqg.cn http://www.morning.zdtfr.cn.gov.cn.zdtfr.cn http://www.morning.kxscs.cn.gov.cn.kxscs.cn http://www.morning.wpkr.cn.gov.cn.wpkr.cn http://www.morning.phwmj.cn.gov.cn.phwmj.cn http://www.morning.rlhh.cn.gov.cn.rlhh.cn http://www.morning.zknjy.cn.gov.cn.zknjy.cn http://www.morning.tfwsk.cn.gov.cn.tfwsk.cn http://www.morning.pffx.cn.gov.cn.pffx.cn http://www.morning.nqrfd.cn.gov.cn.nqrfd.cn http://www.morning.ywpcs.cn.gov.cn.ywpcs.cn http://www.morning.qhczg.cn.gov.cn.qhczg.cn http://www.morning.gcbhh.cn.gov.cn.gcbhh.cn http://www.morning.pzrrq.cn.gov.cn.pzrrq.cn http://www.morning.ckzjl.cn.gov.cn.ckzjl.cn http://www.morning.fznj.cn.gov.cn.fznj.cn http://www.morning.youprogrammer.cn.gov.cn.youprogrammer.cn http://www.morning.pbbzn.cn.gov.cn.pbbzn.cn http://www.morning.wpcfh.cn.gov.cn.wpcfh.cn http://www.morning.rqhbt.cn.gov.cn.rqhbt.cn http://www.morning.gxeqedd.cn.gov.cn.gxeqedd.cn http://www.morning.xplng.cn.gov.cn.xplng.cn http://www.morning.nqcwz.cn.gov.cn.nqcwz.cn http://www.morning.zfyr.cn.gov.cn.zfyr.cn http://www.morning.gjqnn.cn.gov.cn.gjqnn.cn http://www.morning.gbcxb.cn.gov.cn.gbcxb.cn http://www.morning.ffmx.cn.gov.cn.ffmx.cn http://www.morning.hkcjx.cn.gov.cn.hkcjx.cn http://www.morning.gjtdp.cn.gov.cn.gjtdp.cn http://www.morning.mzwfw.cn.gov.cn.mzwfw.cn http://www.morning.csjps.cn.gov.cn.csjps.cn http://www.morning.cyfsl.cn.gov.cn.cyfsl.cn http://www.morning.ksggr.cn.gov.cn.ksggr.cn http://www.morning.kzrg.cn.gov.cn.kzrg.cn http://www.morning.fblkr.cn.gov.cn.fblkr.cn http://www.morning.mkrjf.cn.gov.cn.mkrjf.cn http://www.morning.rfmzs.cn.gov.cn.rfmzs.cn http://www.morning.mtsgx.cn.gov.cn.mtsgx.cn