list简介

简介:链表,是一种在物理存储单元上非连续的存储结构。

list构造函数

函数原型

  • list<T> lst; // list采用模板类实现,对象的默认构造形式
  • list(beg,end);// 构造函数[beg,end)区间中的元素拷贝给本身
  • list(n,elem);// 构造函数将n个elem拷贝被本身
  • list(const list &list);// 拷贝构造函数

测试代码

#inclue<iostream>
using namespace std;
#include<list>// 遍历容器的方法
void PrintList(const list<int>& L)
{for (list<int>::const_iterator it = L.begin(); it != L.end(); it++){cout << *it << " ";}cout << endl;
}
void test01()
{// Create a list container// 创建容器list<int>L1;// Add data // 添加数据L1.push_back(10);L1.push_back(20);L1.push_back(30);L1.push_back(40);// Traverse container// 遍历容器cout << "L1: ";PrintList(L1);cout << endl;// Constructed in the way of interval// 以区间形式拷贝list<int>L2(L1.begin(), L1.end());  cout << "L2: ";PrintList(L2);cout << endl;// Copy constructer // 拷贝构造方式构建list<int>L3(L2);cout << "L3: ";PrintList(L3);cout << endl;// n elements// 构造函数将n个elem拷贝被本身list<int>L4(10, 1000);cout << "L4: ";PrintList(L4);cout << endl;}int main()
{test01();return 0;
}

运行结果

总结:list构造方式同其他几个STL常用容器相似。

Summary: list is similar to several other STL containers.

list 赋值和交换

  • assign(beg,end); // 将[beg,end)区间中的数据拷贝赋值给本身
  • assign(n,elem); // 将n个elem拷贝赋值给本身
  • list& operator=(const list &lst);// 重载等号操作符
  • swap(lst); // 将lst与本身的元素互换
#include<iostream>
#include<list>// Assignment and exchange of list containerusing namespace std;void PrintList(const list<int>& L)
{for (list<int>::const_iterator it = L.begin(); it != L.end(); it++){cout << *it << " ";}cout << endl;
}// Assignment void test01()
{list<int>L1;L1.push_back(10);L1.push_back(20);L1.push_back(30);L1.push_back(40);cout << "L1: ";PrintList(L1);cout << endl;list<int>L2;L2 = L1; // list& operator = (const list & list)cout << "L2: ";PrintList(L2);cout << endl;list<int>L3;// assign(beg,end);  // 将[beg,end)区间中的数据拷贝赋值给本身L3.assign(L2.begin(), L2.end());cout << "L3: ";PrintList(L3);cout << endl;list<int>L4;// assign(n,elem); // 将n个elem拷贝赋值给本身L4.assign(10, 100);cout << "L4: ";PrintList(L4);cout << endl;
}// Swap
void test02()
{list<int>L1;L1.push_back(10);L1.push_back(20);L1.push_back(30);L1.push_back(40);list<int>L2;L2.assign(10, 123);cout << "交换前:" << endl;cout << "L1: ";PrintList(L1);cout << endl;cout << "L2: ";PrintList(L2);cout << endl;cout << "交换后: " << endl;L2.swap(L1);cout << "L1: ";PrintList(L1);cout << endl;cout << "L2: ";PrintList(L2);cout << endl;
}
int main()
{cout << "赋值:" << endl;test01();cout << "交换:\n" << endl;test02();system("pause");return 0;
}

运行结果

list大小操作

功能:对list容器的大小进行操作

函数原型

  • size();// 返回容器中元素的个数

  • empty();// 判断容器是否为空

  • resize(num);// 重新指定容器的长度为num,若容器变长,则以默认值填充新位置

    ​ // 如果容器变短,则末尾超出容器的长度的元素被删除

  • resize(num, elem);// 重新指定容器的长度num,若容器变长,则以elem值填充新位置

    ​ // 如果容器变短,则末尾超出容器长度的元素被删除

#include<iostream>
#include<list>using namespace std;// List container size operation void PrintList(const list<int>& L)
{for (list<int>::const_iterator it = L.begin(); it != L.end(); it++){cout << *it << " ";}cout << endl;
}void test01()
{list<int>L1;L1.push_back(10);L1.push_back(20);L1.push_back(30);L1.push_back(40);cout << "L1: ";PrintList(L1);cout << endl;// Judge whether the container is emptyif (L1.empty()){cout << "L1为空" << endl;}else{cout << "L1不为空" << endl;cout << "L1的元素个数为:" << L1.size() << endl;}cout << endl;// Re sizeL1.resize(10, 10000); cout << "L1: ";PrintList(L1);cout << endl;L1.resize(2);  // Take the first two elements of the listcout << "L1: ";PrintList(L1);cout << endl;
}int main()
{test01();return 0;
}

运行结果

list插入和删除

  • 尾插 — push_back();
  • 尾删 — pop_back();
  • 头插 — push_front();
  • 头删 — pop_front();
  • 插入 — insert();
  • 删除 — erase();
  • 移除 — remove();
  • 清空 — clear();

这里要说明的是list迭代器不能跳跃移动,例如insert函数中的参数写成L.begin() + n的操作是不可以的,只能写成++L.begin(), 或者提前定义一个迭代器变量it然后++it才能访问下一个位置,因为list的指针域指向指向下一个元素,并没有指向下下个元素。

What I want to explain here is that the list iterator cannot jump. For example, the operator of writing the parameter in the insert function as L.begin() + n is not allowed. it can only be written as ++L.begin(), or define an iterator variable “it” in advance, and then ++it can access the next position, because the pointer field of the list points to the next element, not to the next next element.

#include<iostream>
#include<list>// Insertion and deletion of list containerusing namespace std;void PrintList(const list<int>&L)
{for (list<int>::const_iterator it = L.begin(); it != L.end(); it++){cout << *it << " ";}cout << endl;
}void test01()
{list<int>L;// push_back tail insertion// 尾插L.push_back(10);L.push_back(20);L.push_back(30);// push_front head insertion// 头插入L.push_front(100);L.push_front(200);L.push_front(300);// 300 200 100 10 20 30 cout << "L: ";PrintList(L);cout << endl;// pop_back tail deletion // 头插L.pop_back();// 300 200 100 10 20 cout << "L: ";PrintList(L);cout << endl;// pop_head head deletion// 头插L.pop_front();// 200 100 10 20 cout << "L: ";PrintList(L);cout << endl;// Inser Insertion // 索引为1的位置插入L.insert(++L.begin(), 12345);//  200 12345 100 10 20cout << "L: ";PrintList(L);cout << endl;// erase deletion // 删掉索引为1的list<int>::const_iterator it = L.begin();L.erase(++it);// 200 100 10 20;cout << "L: ";PrintList(L);cout << endl;// Remove by value// 尾插L.push_back(10);// 200 100 10 20 10L.remove(10);// 200 100 20cout << "L: ";PrintList(L);cout << endl;}
int main()
{test01();return 0;
}

运行结果

list 数据存取

功能描述:对list容器中数据进行存取

要说明的是因为链表存储不是一段连续线性的空间,list容器不支持直接用at,[],两种方式的访问元素和随机访问,list的迭代器是一个双向迭代器,只支持前移或者后移,不支持跳跃式访问,所以只有front(), back()两个接口,不过可以通过’++'运算符,多加几次找到自己想要的位置,或者自己写个函数,函数里面的原理类似打印时的那个循环,改变迭代器的位置,来达到自己想要的位置.

It should be noted that because the list storage is not a continuous linear space. the list container does not support direct access to elements and random access by at and [] methods.

The iterator of the list is a two-way iterator, which only supports forward or backward movement, and does not support skip access, so it has only two functions front() and back() are two interfaces, but you can use the '++'operator to add several times to find the position

you want, or you can write a function. The principle of the function is similar to the loop when printing, and you can change the position of the iteration to achieve the position you want.

函数原型

  • front();// 返回第一个元素
  • back();// 返回最后一个元素
#include<iostream>
#include<list>using namespace std;/*Data access of list container*/void PrintList(const list<int>& L)
{for (list<int>::const_iterator it = L.begin(); it != L.end(); it++){cout << *it << " ";}cout << endl;
}void test01()
{list<int>L1;L1.push_back(10);L1.push_back(20);L1.push_back(30);L1.push_back(40);cout << "第一个元素为:" << L1.front() << endl;cout << "最后一个元素为:" << L1.back() << endl;// Verification iterators do not support random accesslist<int>::iterator it = L1.begin();// it += 1; it += 5; It's all wrong}
int main()
{test01();system("pause");return 0;
}

运行结果

list反转和排序

功能描述:将容器中的元素翻转,以及将容器中的数据排序。

函数原型

  • reverse();// 反转链表
  • sort();// 链表排序

注意:所有不支持随机访问的迭代器的容器,不可以用标准的排序算法。

不支持随机访问迭代器的容器,内部会提供对应的一些算法.

Note: all containers that do not support random access iterators cannot use standard sorting algorithm.

Containers that do not support random access iterators will provide corresponding algorithm.

#include<iostream>
#include<list>
#include<algorithm>using namespace std;/*Reverse and sort of list container*/bool cmp(int v1, int v2)
{return v1 > v2;
}void PrintList(const list<int>& L)
{for (list<int>::const_iterator it = L.begin(); it != L.end(); it++){cout << *it << " ";}cout << endl;
}void test01()
{list<int>L1;L1.push_back(20);L1.push_back(10);L1.push_back(50);L1.push_back(40);L1.push_back(0);// Reverse cout << "反转前L1: ";PrintList(L1);cout << endl;cout << "反转后L1: ";L1.reverse();PrintList(L1);cout << endl;}void test02()
{list<int>L1;L1.push_back(20);L1.push_back(10);L1.push_back(50);L1.push_back(40);L1.push_back(0);// Sort// You can't write that/*sort(L1.begin(), L1.end());*/L1.sort();  // Default rule: ascending order, from small to largecout << "升序 排序后L1: ";PrintList(L1);cout << endl;L1.sort(cmp);  // You can customize function rulescout << "降序 排序后L1: ";PrintList(L1);cout << endl;
}
int main()
{test01();test02();system("pause");return 0;
}

运行结果

C++_list快速学习入门(中英文结合)相关推荐

  1. JSON快速学习入门

    title: JSON author: CodeCheng top: false cover: false toc: true mathjax: false tags: 前端 json categor ...

  2. Ruby入门之零基础如何学ruby以及ruby的应用/快速学习ruby/学习ruby的流程是什么?...

    作者:李路 链接:https://www.zhihu.com/question/19552402/answer/22336708 来源:知乎 快速学习Ruby on Rails,这应该是个伪命题,没有 ...

  3. 给深度学习入门者的Python快速教程 - 番外篇之Python-OpenCV

    转载自:https://zhuanlan.zhihu.com/p/24425116 本篇是前面两篇教程:给深度学习入门者的Python快速教程 - 基础篇 给深度学习入门者的Python快速教程 - ...

  4. 给深度学习入门者的Python快速教程 - numpy和Matplotlib篇

    转载自:https://zhuanlan.zhihu.com/p/24309547 本篇部分代码的下载地址: https://github.com/frombeijingwithlove/dlcv_f ...

  5. 深度学习入门(一)快速建立自己的图片数据集

    机器学习或深度学习的第一步是获取数据集,一般我们使用业务数据集或公共数据集.本文将介绍使用 Bing Image Search API 和 Python 脚本,快速的建立自己的图片数据集. 1. 快速 ...

  6. 刘海洋 · LaTeX 不快速的入门 学习笔记

    刘海洋 · LaTeX 不快速的入门 学习笔记 网址链接 : 刘海洋 · LaTeX 不快速的入门 - 跟着大神学习最纯正的 LaTeX 知识 一.组织文档结构 1. 文档基本结构 以document ...

  7. Python快速编程入门#学习笔记01# |第一章 :Python基础知识 (Python发展历程、常见的开发工具、import模块导入)

    全文目录 ==先导知识== 1 认识Python 1.1.1 Python的发展历程 1.1.2 Python语言的特点 2. Python解释器的安装与Python程序运行 1.2.1 安装Pyth ...

  8. 准备选择计算机方向,该怎样快速学习电脑知识?零基础到精通入门!

    怎样快速学电脑知识?零基础到精通入门学习,电脑是辅助人们工作的工具,想要熟练使用电脑就需要掌握相应的操作方法,对应零基础对电脑一窍不通的人,我们该怎么快速学习电脑操作呢? 怎样快速学电脑知识? 一.确 ...

  9. Python快速编程入门#学习笔记03# |第二章 :Python基础(代码格式、标识符关键字、变量和数据类型、数字类型以及运算符)

    全文目录 ==先导知识== 学习目标: 2.1 代码格式 2.1.1 注释 2.1.2 缩进 2.1.3 语句换行 2.2 标识符和关键字 2.2.1 标识符 2.2.2 关键字 2.3 变量和数据类 ...

最新文章

  1. Python - selenium_WebDriver 鼠标键盘事件
  2. linux的tar中ztvf,linux中的tar命令(2)
  3. Mongodb 添加删除分片与非分片表维护
  4. 学计算机的要做文档吗,我是如何学习计算机编程的
  5. 阿里技术:万级规模K8s如何管理?
  6. (需求实战_进阶_02)SSM集成RabbitMQ 关键代码讲解、开发、测试
  7. 单体 soa 微服务 区别_每日一读-从单体到微服务,这些年架构的演变
  8. NeurIPS 2021揭榜,接收率创九年新高,论文列表已公布,你的文章中了吗?
  9. DHCP原理及报文格式
  10. 【BZOJ1500】[NOI2005]维修数列
  11. C# 短视频 无水印解析 原视频下载(超详细)
  12. 遗传算法(geatpy)
  13. matlab梯形法求二重积分,复化梯形公式公式求二重积分matlab源码
  14. 微信小程序 宠物社区源码
  15. Android studio成品 记账本(附带文档)
  16. vant框架的输入框在IOS上出现输入空格不显示,需要在输入字符才展示问题
  17. Linux kernel的中断子系统之(二):IRQ Domain介绍
  18. IBM计算机启动过程,ibm台式机bios设置u盘启动教程【图文教程】
  19. R语言将数据导出到csv时出现科学计数表示
  20. 1Curiosity--NASA's Mission to Mars 2019/11/05F450KLG2H

热门文章

  1. android app实现系统导航栏设置图片起到沉浸式效果
  2. 如何通过python画loss曲线 点线颜色及点线型设置说明(超实用)
  3. 计算机体系结构控制相关实验,实验室简介--中科院计算所计算机体系结构国家重点实验室...
  4. nRF51822 入门必备教程(一篇搞定nRF51)
  5. 3dmax2016201520142013vary渲染视频教程从入门到精通
  6. git一键克隆多个仓库
  7. 遴选国际一流期刊的评价指标
  8. java相册管理_基于jsp的相册管理系统-JavaEE实现相册管理系统 - java项目源码
  9. PHM健康评估建模方法
  10. 雾计算与边缘计算的区别