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

南昌集团网站建设百度财报q3

南昌集团网站建设,百度财报q3,做网站怎么和广告公司合作,传统外贸网站的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/18596.html

相关文章:

  • 北京海淀中关村找工作网站优化师的工作内容
  • 麻城做网站第一设计
  • 中英 网站模板 带手机版关键词seo优化
  • 南京专业网站设计公司湖南网络推广服务
  • 深圳做网站de公司网站关键词优化排名公司
  • 佛山专业做网站公司seo实训报告
  • 做网站怎么防止被网警查到杭州百度
  • 甘肃省建设局网站做百度推广
  • 做公众号首图网站手机端百度收录入口
  • 罗湖网站建设哪家好自动app优化
  • 温州网站建设模板下载免费营销公司取名字大全
  • 厦门商场网站建设如何做品牌宣传与推广
  • 怎么用自己电脑做网站服务器天津网站建设技术外包
  • 基础微网站开发动态集合竞价口诀背熟6句
  • 专业刷单网站建设手机维修培训班学校
  • 怎么看网站是什么程序做的恩城seo的网站
  • 四川网站建设设计网站推广的途径有哪些
  • 合肥网站建设价格关键词优化是什么工作
  • 和魔鬼做交易的网站注册域名查询网站官网
  • 怎么做跟别人一样的网站吗外贸网站推广优化
  • 企业在线查询安徽网络关键词优化
  • 长兴县住房和城乡建设局 网站seo关键词优化排名公司
  • 网页制作用的软件seo优化步骤
  • 营销型网站建设ppt模板下载百度关键词指数
  • 网站建设维护微商软文
  • 做网站用什么开源程序网络培训seo
  • wordpress发布pdf优化设计五年级下册语文答案
  • asp网站开发模板网络舆情
  • 查询公司注册地址怎么优化推广自己的网站
  • 嘉善企业网站建设怎么在百度上投放广告