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

网站建设和电子商务的关系php开源建站系统

网站建设和电子商务的关系,php开源建站系统,我的世界做壁纸网站打不开,做胃肠科网站目录 list是什么 构造函数 元素访问 容量操作 修改 迭代器 code实例 实现简单的list forward_list是什么 构造函数 元素访问 容量 修改 迭代器 code实例 实现一个简单的forward_list list是什么 std::list 是 C 标准模板库(STL)中的一个…

目录

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()默认构造函数,创建一个空的 list
list(size_type count)创建包含 count 个默认初始化元素的 list。
list(size_type count, const T& value)创建包含 count 个值为 value 的元素的 list
list(const list& other)拷贝构造函数
list(initializer_list<T> 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 的大小为 count
insert(iterator pos, const T& value): 在指定位置 pos 插入元素 value
push_back(const T& value): 在 list 的末尾添加元素 value
push_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 <iostream>int main() {//构造函数std::list<int> l1; // 空 liststd::list<int> l2(5); // 包含 5 个默认初始化元素的 liststd::list<int> l3(5, 10); // 包含 5 个值为 10 的元素的 liststd::list<int> 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 <iostream>template <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() {SimpleList<int> 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_list
forward_list(size_type count)创建包含 count 个默认初始化元素的 forward_list
forward_list(size_type count, const T& value)创建包含 count 个值为 value 的元素的 forward_list
forward_list(const forward_list& other)拷贝构造函数
forward_list(initializer_list<T> 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 的开头添加元素 value
resize(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 <iostream>int main() {//构造函数std::forward_list<int> fl1; // 空 forward_liststd::forward_list<int> fl2(5); // 包含 5 个默认初始化元素的 forward_liststd::forward_list<int> fl3(5, 10); // 包含 5 个值为 10 的元素的 forward_liststd::forward_list<int> 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 <iostream>template <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() {SimpleForwardList<int> fl;fl.push_front(1);fl.push_front(2);fl.push_front(3);fl.print(); // 输出: 3 2 1return 0;
}

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

相关文章:

  • 网站设计参考文献有哪些武汉seo公司哪家专业
  • 乐山公司网络建设百度seo优化策略
  • 上海宝山网站建设培训班今晚赛事比分预测
  • 中国和城乡建设部网站制作一个网站的费用是多少
  • 上海网站开发制作网站建设知名公司
  • 网站语言选择上海搜索引擎优化公司排名
  • 福田的网站建设公司哪家好百度一下打开网页
  • 企业网站 cms搜狗排名优化工具
  • 基于web网站开发对网络营销的理解
  • 哪些网站用echarts做的市场调研方案范文
  • wordpress 漫画在线长春网站seo哪家好
  • 有没有网站开发软件怎么知道自己的域名
  • wordpress地图在哪seo网站推广收费
  • 网站调研怎样做地推拉新app推广怎么做
  • 做英文网站用什么源码重庆seo网站
  • seo网站推广方案深圳百度快速排名优化
  • 阿里云的网站建设花钱么百度公司官网首页
  • 职业本科专业建设规划上海seo服务
  • 企业百度网站怎么做的免费制作网站
  • 个人申请公司流程宁波seo公司网站推广
  • 网站官网建设大数据培训班出来能就业吗
  • 网站建设维护属于什么专业2022新闻大事件摘抄
  • 单位网站建设管理情况seo优化包括什么
  • 如何建设网站哪个济南兴田德润简介开发一个app平台大概需要多少钱?
  • 东莞网站建设环保设备营销方式和手段
  • 建设部证书查询官方网站写软文平台
  • 莆田网站建设哪里便宜2345网址导航怎么样
  • 怎么做网站注册推广百度店铺免费入驻
  • 手机网站制作公司 广州今日军事新闻最新消息
  • 帝国网站管理系统前台域名免费注册0元注册