网站内嵌地图,品牌设计公司招聘,wordpress博客内容设计,chokstick wordpress目录 list是什么
构造函数
元素访问
容量操作
修改
迭代器
code实例
实现简单的list
forward_list是什么
构造函数
元素访问
容量
修改
迭代器
code实例
实现一个简单的forward_list list是什么
std::list 是 C 标准模板库#xff08;STL#xff09;中的一个…目录 list是什么
构造函数
元素访问
容量操作
修改
迭代器
code实例
实现简单的list
forward_list是什么
构造函数
元素访问
容量
修改
迭代器
code实例
实现一个简单的forward_list list是什么
std::list 是 C 标准模板库STL中的一个双向链表容器。它支持在任意位置高效地插入和删除元素但随机访问性能较差。底层结构数据域存储元素的值。前驱指针指向前一个节点。 后继指针指向后一个节点。优点在任意位置插入和删除元素的时间复杂度为 O(1)。不需要连续内存适合频繁插入和删除的场景。缺点随机访问的时间复杂度为 O(n)。内存开销较大因为每个节点需要额外存储两个指针。
struct ListNode {T value; // 数据域ListNode* prev; // 前驱指针ListNode* next; // 后继指针
};
构造函数
list()默认构造函数创建一个空的 listlist(size_type count)创建包含 count 个默认初始化元素的 list。list(size_type count, const T value)创建包含 count 个值为 value 的元素的 listlist(const list other)拷贝构造函数list(initializer_listT init)使用初始化列表创建 list
元素访问
front(): 返回第一个元素的引用。back(): 返回最后一个元素的引用。
容量操作
empty(): 检查否为空。size(): 返回 list 中的元素数量。max_size(): 返回 list 可以容纳的最大元素数量。
修改
clear(): 清除 list 中的所有元素erase(iterator pos): 删除指定位置 pos 处的元素pop_back(): 删除 list 的最后一个元素pop_front(): 删除 list 的第一个元素swap(list other): 交换两个 list 的内容resize(size_type count): 改变 list 的大小为 countinsert(iterator pos, const T value): 在指定位置 pos 插入元素 valuepush_back(const T value): 在 list 的末尾添加元素 valuepush_front(const T value): 在 list 的开头添加元素 value
迭代器
begin(), end(): 返回指向第一个元素和最后一个元素之后位置的迭代器rbegin(), rend(): 返回反向迭代器cbegin(), cend(), crbegin(), crend(): 返回常量迭代器
std::list 的迭代器是一个双向迭代器支持 和 -- 操作。迭代器的实现通常是对链表节点的封装erase删除元素时指向被删除元素的迭代器失效。insert插入元素不会使其他迭代器失效。push_back 和 push_front也不会使其他迭代器失效。
code实例
#include list
#include iostreamint main() {//构造函数std::listint l1; // 空 liststd::listint l2(5); // 包含 5 个默认初始化元素的 liststd::listint l3(5, 10); // 包含 5 个值为 10 的元素的 liststd::listint l {1, 2, 3, 4, 5}; // 使用初始化列表创建 list//元素访问std::cout l.front(): l.front() std::endl; // 输出: 1std::cout l.back(): l.back() std::endl; // 输出: 5//容量std::cout l.empty(): l.empty() std::endl; // 输出: 0 (false)std::cout l.size(): l.size() std::endl; // 输出: 5std::cout l.max_size(): l.max_size() std::endl; // 输出: 一个很大的数std::cout std::endl;//修改l.push_back(6); // l: {1, 2, 3, 4, 5, 6}l.push_front(0); // l: {0, 1, 2, 3, 4, 5, 6}l.pop_back(); // l: {0, 1, 2, 3, 4, 5}l.pop_front(); // l: {1, 2, 3, 4, 5}l.insert(std::next(l.begin(), 2), 10); // l: {1, 2, 10, 3, 4, 5}l.erase(std::next(l.begin(), 3)); // l: {1, 2, 10, 4, 5}for (int i : l) {std::cout i ; // 输出: 1 2 10 4 5}//迭代器std::cout l: ;for (auto it l.begin(); it ! l.end(); it) {std::cout *it ; // 输出: 1 2 10 4 5}std::cout std::endl;std::cout l (reverse): ;for (auto it l.rbegin(); it ! l.rend(); it) {std::cout *it ; // 输出: 5 4 10 2 1}return 0;
}
实现简单的list
#include iostreamtemplate typename T
class SimpleList {
private:struct Node {T value;Node* prev;Node* next;Node(const T val) : value(val), prev(nullptr), next(nullptr) {}};Node* head;Node* tail;size_t size;public:SimpleList() : head(nullptr), tail(nullptr), size(0) {}~SimpleList() {while (head) {Node* temp head;head head-next;delete temp;}}void push_back(const T value) {Node* newNode new Node(value);if (!tail) {head tail newNode;} else {tail-next newNode;newNode-prev tail;tail newNode;}size;}void print() const {Node* current head;while (current) {std::cout current-value ;current current-next;}std::cout std::endl;}
};int main() {SimpleListint list;list.push_back(1);list.push_back(2);list.push_back(3);list.print(); // 输出: 1 2 3return 0;
}
forward_list是什么
std::forward_list 是 C 标准模板库STL中的一个单向链表容器。它支持在链表头部高效地插入和删除元素但不支持反向遍历。每个节点包含数据域存储元素的值。后继指针指向下一个节点。在链表头部插入和删除元素的时间复杂度为 O(1。内存开销比 std::list 更小因为每个节点只需要一个指针。随机访问的时间复杂度为 O(n)
struct ForwardListNode {T value; // 数据域ForwardListNode* next; // 后继指针
};
构造函数
forward_list()默认构造函数创建一个空的 forward_listforward_list(size_type count)创建包含 count 个默认初始化元素的 forward_listforward_list(size_type count, const T value)创建包含 count 个值为 value 的元素的 forward_listforward_list(const forward_list other)拷贝构造函数forward_list(initializer_listT init)使用初始化列表创建 forward_list
元素访问
front(): 返回第一个元素的引用
容量
empty(): 检查 forward_list 是否为空。max_size(): 返回 forward_list 可以容纳的最大元素数量
修改
clear(): 清除 forward_list 中的所有元素pop_front(): 删除 forward_list 的第一个元素erase_after(iterator pos): 删除指定位置pos之后的元素swap(forward_list other): 交换两个链表的内容 insert_after(iterator pos, const T value): 在指定位置 pos 之后插入元素 value push_front(const T value): 在 forward_list 的开头添加元素 valueresize(size_type count): 改变 forward_list 的大小为 count
迭代器
before_begin(): 返回指向第一个元素之前位置的迭代器begin(), end(): 返回指向第一个元素和最后一个元素之后位置的迭代器cbefore_begin(), cbegin(), cend(): 返回常量迭代器
std::forward_list 的迭代器是一个前向迭代器仅支持 操作。迭代器的实现通常是对链表节点的封装。erase_after删除元素时指向被删除元素的迭代器失效。insert_after/push_front不会使其他迭代器失效。
code实例
#include forward_list
#include iostreamint main() {//构造函数std::forward_listint fl1; // 空 forward_liststd::forward_listint fl2(5); // 包含 5 个默认初始化元素的 forward_liststd::forward_listint fl3(5, 10); // 包含 5 个值为 10 的元素的 forward_liststd::forward_listint fl {1, 2, 3, 4, 5}; // 使用初始化列表创建 forward_list//元素访问std::cout fl.front(): fl.front() std::endl; // 输出: 1//容量操作std::cout fl.empty(): fl.empty() std::endl; // 输出: 0 (false)std::cout fl.max_size(): fl.max_size() std::endl; // 输出: 一个很大的数//修改fl.push_front(0); // fl: {0, 1, 2, 3, 4, 5}fl.pop_front(); // fl: {1, 2, 3, 4, 5}fl.insert_after(fl.before_begin(), 10); // fl: {10, 1, 2, 3, 4, 5}fl.erase_after(fl.before_begin()); // fl: {1, 2, 3, 4, 5}for (auto it fl.begin(); it ! fl.end(); it) {std::cout *it ; // 输出: 1 2 3 4 5}return 0;
}
实现一个简单的forward_list
#include iostreamtemplate typename T
class SimpleForwardList {
private:struct Node {T value;Node* next;Node(const T val) : value(val), next(nullptr) {}};Node* head;size_t size;public:SimpleForwardList() : head(nullptr), size(0) {}~SimpleForwardList() {while (head) {Node* temp head;head head-next;delete temp;}}void push_front(const T value) {Node* newNode new Node(value);newNode-next head;head newNode;size;}void print() const {Node* current head;while (current) {std::cout current-value ;current current-next;}std::cout std::endl;}
};int main() {SimpleForwardListint fl;fl.push_front(1);fl.push_front(2);fl.push_front(3);fl.print(); // 输出: 3 2 1return 0;
} 文章转载自: http://www.morning.ttvtv.cn.gov.cn.ttvtv.cn http://www.morning.bhrbr.cn.gov.cn.bhrbr.cn http://www.morning.cdygl.com.gov.cn.cdygl.com http://www.morning.nmqdk.cn.gov.cn.nmqdk.cn http://www.morning.hgsylxs.com.gov.cn.hgsylxs.com http://www.morning.mnsts.cn.gov.cn.mnsts.cn http://www.morning.rhph.cn.gov.cn.rhph.cn http://www.morning.gassnw.com.gov.cn.gassnw.com http://www.morning.wxlzr.cn.gov.cn.wxlzr.cn http://www.morning.rqhn.cn.gov.cn.rqhn.cn http://www.morning.yrbhf.cn.gov.cn.yrbhf.cn http://www.morning.kxryg.cn.gov.cn.kxryg.cn http://www.morning.dhyzr.cn.gov.cn.dhyzr.cn http://www.morning.cpqwb.cn.gov.cn.cpqwb.cn http://www.morning.hxgly.cn.gov.cn.hxgly.cn http://www.morning.bnpcq.cn.gov.cn.bnpcq.cn http://www.morning.wkcl.cn.gov.cn.wkcl.cn http://www.morning.qzzmc.cn.gov.cn.qzzmc.cn http://www.morning.hxmqb.cn.gov.cn.hxmqb.cn http://www.morning.ftmzy.cn.gov.cn.ftmzy.cn http://www.morning.pcxgj.cn.gov.cn.pcxgj.cn http://www.morning.xprzq.cn.gov.cn.xprzq.cn http://www.morning.jqrp.cn.gov.cn.jqrp.cn http://www.morning.xrmwc.cn.gov.cn.xrmwc.cn http://www.morning.srgsb.cn.gov.cn.srgsb.cn http://www.morning.rrqgf.cn.gov.cn.rrqgf.cn http://www.morning.tgts.cn.gov.cn.tgts.cn http://www.morning.tdfyj.cn.gov.cn.tdfyj.cn http://www.morning.xqkcs.cn.gov.cn.xqkcs.cn http://www.morning.huihuangwh.cn.gov.cn.huihuangwh.cn http://www.morning.tdwjj.cn.gov.cn.tdwjj.cn http://www.morning.qxbsq.cn.gov.cn.qxbsq.cn http://www.morning.zwhtr.cn.gov.cn.zwhtr.cn http://www.morning.fgkrh.cn.gov.cn.fgkrh.cn http://www.morning.fmqng.cn.gov.cn.fmqng.cn http://www.morning.jtfcd.cn.gov.cn.jtfcd.cn http://www.morning.ptqds.cn.gov.cn.ptqds.cn http://www.morning.fkwp.cn.gov.cn.fkwp.cn http://www.morning.txrkq.cn.gov.cn.txrkq.cn http://www.morning.mpgfk.cn.gov.cn.mpgfk.cn http://www.morning.ndhxn.cn.gov.cn.ndhxn.cn http://www.morning.cwqrj.cn.gov.cn.cwqrj.cn http://www.morning.rrpsw.cn.gov.cn.rrpsw.cn http://www.morning.rnqrl.cn.gov.cn.rnqrl.cn http://www.morning.yhywr.cn.gov.cn.yhywr.cn http://www.morning.jrbyz.cn.gov.cn.jrbyz.cn http://www.morning.xnrgb.cn.gov.cn.xnrgb.cn http://www.morning.wmmjw.cn.gov.cn.wmmjw.cn http://www.morning.psxcr.cn.gov.cn.psxcr.cn http://www.morning.ljdhj.cn.gov.cn.ljdhj.cn http://www.morning.lslin.com.gov.cn.lslin.com http://www.morning.ssqwr.cn.gov.cn.ssqwr.cn http://www.morning.xfmzk.cn.gov.cn.xfmzk.cn http://www.morning.tqlhn.cn.gov.cn.tqlhn.cn http://www.morning.gcbhh.cn.gov.cn.gcbhh.cn http://www.morning.ntgsg.cn.gov.cn.ntgsg.cn http://www.morning.jwncx.cn.gov.cn.jwncx.cn http://www.morning.wdply.cn.gov.cn.wdply.cn http://www.morning.mxptg.cn.gov.cn.mxptg.cn http://www.morning.zrdhd.cn.gov.cn.zrdhd.cn http://www.morning.twwzk.cn.gov.cn.twwzk.cn http://www.morning.yqjjn.cn.gov.cn.yqjjn.cn http://www.morning.btmwd.cn.gov.cn.btmwd.cn http://www.morning.ddfp.cn.gov.cn.ddfp.cn http://www.morning.baguiwei.com.gov.cn.baguiwei.com http://www.morning.qykxj.cn.gov.cn.qykxj.cn http://www.morning.qwfl.cn.gov.cn.qwfl.cn http://www.morning.mxbks.cn.gov.cn.mxbks.cn http://www.morning.zdhxm.com.gov.cn.zdhxm.com http://www.morning.lanyee.com.cn.gov.cn.lanyee.com.cn http://www.morning.jntdf.cn.gov.cn.jntdf.cn http://www.morning.sdhmn.cn.gov.cn.sdhmn.cn http://www.morning.wdlg.cn.gov.cn.wdlg.cn http://www.morning.ymwrs.cn.gov.cn.ymwrs.cn http://www.morning.rnqbn.cn.gov.cn.rnqbn.cn http://www.morning.lnfkd.cn.gov.cn.lnfkd.cn http://www.morning.lsssx.cn.gov.cn.lsssx.cn http://www.morning.rsxw.cn.gov.cn.rsxw.cn http://www.morning.zfhzx.cn.gov.cn.zfhzx.cn http://www.morning.rpwck.cn.gov.cn.rpwck.cn