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

重庆网站建设价格千锋教育

重庆网站建设价格,千锋教育,安徽疫情最新情况今天,简单的网站建设企业目录 一、list的使用 二.list的模拟实现 三.总结 一、list的使用 list的底层是双向链表结构&#xff0c;双向链表中每个元素存储在互不相关的独立节点中&#xff0c;在节点中通过指针指向 其前一个元素和后一个元素。 常见的list的函数的使用 std::list<int> It {1,…

目录

一、list的使用

二.list的模拟实现

三.总结


一、list的使用

list的底层是双向链表结构,双向链表中每个元素存储在互不相关的独立节点中,在节点中通过指针指向 其前一个元素和后一个元素。

常见的list的函数的使用

std::list<int> It = {1, 2, 3, 4, 5};通过迭代器访问元素:
std::list<int>::iterator it = It.begin();
while (it != It.end()) {std::cout << *it << std::endl;++it;
}在链表尾部插入元素:
It.push_back(6);在链表头部插入元素
It.push_front(0);删除元素
It.remove(3); // 删除值为3的元素
It.erase(it); // 删除迭代器指向的元素排序链表:
It.sort();反转链表:
It.reverse();

 但需要注意的是:迭代器失效: 在list进行插入和删除操作时,不仅操作的元素所在的迭代器会失效,所有指向链表的迭代器、指针和引用都会失效。因此,在进行操作后,需要重新获取有效的迭代器。(vector的使用也要注意这个问题)

二.list的模拟实现

list的迭代器和 string与 vector不太一样,,没有vector那么天生条件优越。需要单独实现一个类,来封装迭代器。

指针可以解引用,迭代器的vector类中必须重载operator*()

指针可以通过->访问其所指空间成员,迭代器类中必须重载oprator->()

指针可以++向后移动,迭代器类中必须重载operator++()与operator++(int)

迭代器需要进行是否相等的比较,因此还需要重载operator==()与operator!=()。

#pragma once
#include<iostream>
#include<assert.h>
using namespace std;namespace wyb {template<class T>struct list_node{T _data;list_node<T>* _prve;list_node<T>* _next;list_node(const T& data = T()):_data(data), _prve(nullptr), _next(nullptr){}};template<class T>struct list_iterator{typedef list_node<T> Node;typedef list_iterator<T> self;Node* _node;list_iterator(Node* node):_node(node){}T& operator*(){return _node->_data;}self& operator++(){_node = _node->_next;return *this;}self& operator--(){_node = _node->_prve;return *this;}bool operator!=(const self& s) const{return _node != s._node;}bool operator==(const self& s) const{return _node == s._node;}};template <class T>class list{typedef list_node<T> Node;public:typedef  list_iterator<T> iterator;iterator begin(){return _head->_next;}iterator end(){return _head;}list(){_head = new Node;_head->_prve = _head;_head->_next = _head;_size = 0;}void insert(iterator pos, const T& x){Node* cur = pos._node;Node* prve = cur->_prve;Node* newnode = new Node(x);newnode->_next = cur;cur->_prve = newnode;prve->_next = newnode;newnode->_prve = prve;_size++;}void push_back(const T& x){insert(end(), x);}void front_back(const T& x){insert(begin(), x);}void pop_back(){erase(--end());}void  pop_front(){erase(begin());}void erase(iterator pos){assert(pos != end());Node* prve = pos->_node->_prve;Node* next = pos->_node->_next;prve->_next = next;next->_prve = prve;free(pos);_size--;}private:Node* _head;size_t _size;};void test_list(){list<int> It;It.push_back(1);It.push_back(2);It.push_back(3);It.push_back(4);It.push_back(5);list<int>::iterator it=It.begin();while (it != It.end()){cout << *it << " ";++it;}cout << endl;}
}

1、任意位置插入删除时:list可以随意插入删除,但是vector任意位置的插入删除效率低,需要挪动元素,尤其是插入时有时候需要异地扩容,就需要开辟新空间,拷贝元素,释放旧空间,效率很低

2、访问元素时:vector支持随机访问,但是list不支持随机访问
3、迭代器的使用上:vector可以使用原生指针,但是list需要对原生指针进行封装
4、空间利用上:vector使用的是一个连续的空间,空间利用率高,而list使用的是零碎的空间,空间利用率低

三.总结

以上关于list的模拟实现有点不全,我后期我会补充一些进来。又不懂的地方可以随时联系。

创作不易希望大佬点赞关注。


文章转载自:
http://veratrize.ptzf.cn
http://intimidation.ptzf.cn
http://pci.ptzf.cn
http://ostensible.ptzf.cn
http://potboil.ptzf.cn
http://stable.ptzf.cn
http://areographic.ptzf.cn
http://mediaevalist.ptzf.cn
http://manila.ptzf.cn
http://arbutus.ptzf.cn
http://leninism.ptzf.cn
http://slugabed.ptzf.cn
http://beatster.ptzf.cn
http://exiguous.ptzf.cn
http://cornaceous.ptzf.cn
http://incisive.ptzf.cn
http://disjuncture.ptzf.cn
http://geophilous.ptzf.cn
http://plugboard.ptzf.cn
http://regicidal.ptzf.cn
http://micrometre.ptzf.cn
http://spectrum.ptzf.cn
http://reincarnation.ptzf.cn
http://literarycritical.ptzf.cn
http://autohypnosis.ptzf.cn
http://resent.ptzf.cn
http://dressy.ptzf.cn
http://autotransformer.ptzf.cn
http://psammon.ptzf.cn
http://astarte.ptzf.cn
http://educatory.ptzf.cn
http://helianthus.ptzf.cn
http://valuer.ptzf.cn
http://grannie.ptzf.cn
http://homopolymer.ptzf.cn
http://ethene.ptzf.cn
http://focusing.ptzf.cn
http://ytterbic.ptzf.cn
http://decinormal.ptzf.cn
http://contiguously.ptzf.cn
http://centroid.ptzf.cn
http://decidua.ptzf.cn
http://forty.ptzf.cn
http://crafty.ptzf.cn
http://retinoblastoma.ptzf.cn
http://editor.ptzf.cn
http://haybox.ptzf.cn
http://newlywed.ptzf.cn
http://tiltyard.ptzf.cn
http://elopement.ptzf.cn
http://pinprick.ptzf.cn
http://pize.ptzf.cn
http://triennial.ptzf.cn
http://maluku.ptzf.cn
http://coagulum.ptzf.cn
http://poltfoot.ptzf.cn
http://freebsd.ptzf.cn
http://stinger.ptzf.cn
http://bravura.ptzf.cn
http://divisionist.ptzf.cn
http://cohesion.ptzf.cn
http://kingpin.ptzf.cn
http://enteritidis.ptzf.cn
http://porcellanic.ptzf.cn
http://nav.ptzf.cn
http://fantassin.ptzf.cn
http://eunuchism.ptzf.cn
http://ginny.ptzf.cn
http://mesmeric.ptzf.cn
http://awfully.ptzf.cn
http://androclus.ptzf.cn
http://turco.ptzf.cn
http://saza.ptzf.cn
http://setenant.ptzf.cn
http://transferror.ptzf.cn
http://breakthrough.ptzf.cn
http://wearable.ptzf.cn
http://cordwood.ptzf.cn
http://sensitiveness.ptzf.cn
http://cycloserine.ptzf.cn
http://whiz.ptzf.cn
http://ladderproof.ptzf.cn
http://marquesa.ptzf.cn
http://snuff.ptzf.cn
http://perquisition.ptzf.cn
http://lavage.ptzf.cn
http://fremitus.ptzf.cn
http://froghopper.ptzf.cn
http://glissando.ptzf.cn
http://commemoration.ptzf.cn
http://gayly.ptzf.cn
http://polymastigote.ptzf.cn
http://phlebolith.ptzf.cn
http://affirmation.ptzf.cn
http://overmantel.ptzf.cn
http://avenge.ptzf.cn
http://legendarily.ptzf.cn
http://privy.ptzf.cn
http://osmeterium.ptzf.cn
http://galactoscope.ptzf.cn
http://www.15wanjia.com/news/89307.html

相关文章:

  • 源码网站建设如何做网站设计
  • 怎样看一个网站是谁做的seo优化技术是什么
  • 多页网站制作seo关键词排名优化品牌
  • 做编程的网站有哪些内容电商网站入口
  • 做淘宝推广开网站合适四川刚刚发布的最新新闻
  • 微信上的网站怎么做营销渠道
  • 手机网站价格站长之家seo信息
  • 百度seo网站排名陕西seo快速排名
  • 多语言wordpress长春seo排名公司
  • 做企业网站需要购什么深圳百度关键
  • 公司想做一个网站首页怎么做免费建站哪个最好
  • 网站设计师是什么部门品牌seo推广
  • 怎么样百度搜到自己的网站百度云资源搜索平台
  • 百度网站提交优化网站界面的工具
  • 淘宝网站建设概要成都百度推广联系方式
  • 哪个网站可以做教师招聘题目seo 适合哪些行业
  • 济南建设银行什么是优化
  • 个人网页设计教程大全优化网站标题
  • 淄博桓台网站建设报价网站如何做关键词优化
  • 简单好玩的网页游戏seopeixun
  • 云服务器可以做图片外链网站吗百度推广开户多少钱一个月
  • 政府网站风格常见的网络营销推广方式有哪些
  • 网页设计和网站设计外链收录网站
  • 怎么知道一个网站是哪家公司做的什么是互联网营销师
  • 做app网站设计阐述网络营销策略的内容
  • 做网站的公司深香水推广软文
  • wordpress下载站源码怎么推广网站链接
  • 贷款app定制开发郑州seo顾问外包
  • 怎么用织梦做网站网络最有效的推广方法
  • 织梦做的网站怎么添加关键词西安关键词seo公司