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

企业网站建设企业福建seo快速排名优化

企业网站建设企业,福建seo快速排名优化,做网站毕业设计存在的问题,企业展厅公司哪家好目录 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.dtcms.com/wzjs/513247.html

相关文章:

  • 网站建设成本估算有什么推广产品的渠道
  • 如何用自己网站做大电商沧州搜索引擎优化
  • 网站建设文字内容游戏代理免费加盟
  • 西安做网站好的公司免费自助建站
  • 家装公司网站开发方案互联网营销的特点
  • godaddy 域名交易seo网络优化教程
  • 有空间有域名怎么做网站百度指数预测
  • 做彩票网站用什么服务器网络营销战略
  • 网站建设除了中企动力如何建立网址
  • 外贸建网站免费的黄冈网站有哪些平台
  • 装修房子找哪家装修公司好太原seo自媒体
  • 做网站编程时容易遇到的问题游戏优化大师官网
  • 行政机关单位网站建设要求百度营业执照怎么办理
  • 社交网站设计今天重大新闻头条新闻
  • 杭州房产信息网免费seo教程资源
  • 中国建设银行三峡分行网站泰州seo网络公司
  • 企业网站建设招标评分表游戏推广话术技巧
  • 单页式网站 seo日本比分预测最新分析
  • 有没有免费网站制作竞彩足球最新比赛
  • php 网站开发电商运营工资大概多少
  • 如何建设网站吸引人永久免费客服系统有哪些软件
  • 可靠的广州做网站合肥seo推广公司
  • 网站建设推广方案策划书网页优化方法
  • wordpress哪个主题好青岛seo网络优化公司
  • vs用户登录注册网站建设代码百度商业账号登录
  • 怎么样推销自己网站新网seo关键词优化教程
  • 河北企业建站兰州模板网站seo价格
  • 网页源代码怎么打开快捷键seo推广策划
  • 自己电脑做网站 外网无法访问和生活app下载安装最新版
  • 南宁手机网站制作公司营销型网站建设推广