ist.size();

//返回容器中元素的个数

 1 #include <iostream>
2 #include <list>
3
4 using namespace std;
5
6 int main()
7 {
8 int num[] = { 111,222,333,444,555 };
9 list<int> listInt_A(num, num + size(num));
10
11 cout << "容器中的元素数量为:" << listInt_A.size() << endl;
12
13 return 0;
14 }

打印结果:

list.empty();

//判断容器是否为空

 1 #include <iostream>
2 #include <list>
3
4 using namespace std;
5
6 int main()
7 {
8 int num[] = { 111,222,333,444,555 };
9 list<int> listInt_A(num, num + size(num));
10 list<int> listInt_B;
11
12 if (listInt_A.empty()) //如果不为空将会返回false,如果为空返回 ture
13 {
14 cout << "listInt_A 容器为空" << endl;
15 }
16 else
17 {
18 cout << "listInt_A 容器不为空" << endl;
19 }
20
21 if (listInt_B.empty()) //如果不为空将会返回false,如果为空返回 ture
22 {
23 cout << "listInt_B 容器为空" << endl;
24 }
25 else
26 {
27 cout << "listInt_B 容器不为空" << endl;
28 }
29
30 return 0;
31 }

打印结果:

list.resize(num);

//重新指定容器的长度为num,若容器变长,则以默认值0填充新位置。如果容器变短,则末尾超出容器长度的元素被删除

 1 #include <iostream>
2 #include <list>
3
4 using namespace std;
5
6 int main()
7 {
8 int num[] = { 111,222,333,444,555 };
9 list<int> listInt_A(num, num + size(num));
10
11 cout << "resize 前遍历 listInt_A:" << endl;
12 for (list<int>::iterator it = listInt_A.begin(); it != listInt_A.end(); it++)
13 {
14 cout << *it << " ";
15 }
16 cout << endl;
17
18 //使用 resize 重新指定容器长度
19 listInt_A.resize(9);
20 cout << "resize 扩充至9个元素后遍历 listInt_A:" << endl;
21 for (list<int>::iterator it = listInt_A.begin(); it != listInt_A.end(); it++)
22 {
23 cout << *it << " ";
24 }
25 cout << endl;
26
27 //使用 resize 重新指定容器长度
28 listInt_A.resize(3);
29 cout << "resize 删减至3个元素后遍历 listInt_A:" << endl;
30 for (list<int>::iterator it = listInt_A.begin(); it != listInt_A.end(); it++)
31 {
32 cout << *it << " ";
33 }
34 cout << endl;
35
36 return 0;
37 }

打印结果:

list.resize(num, elem);

//重新指定容器的长度为num,若容器变长,则以elem值填充新位置。如果容器变短,则末尾超出容器长度的元素被删除

 1 #include <iostream>
2 #include <list>
3
4 using namespace std;
5
6 int main()
7 {
8 int num[] = { 111,222,333,444,555 };
9 list<int> listInt_A(num, num + size(num));
10
11 cout << "resize 前遍历 listInt_A:" << endl;
12 for (list<int>::iterator it = listInt_A.begin(); it != listInt_A.end(); it++)
13 {
14 cout << *it << " ";
15 }
16 cout << endl;
17
18 //使用 resize 重新指定容器长度
19 listInt_A.resize(9, 888);
20 cout << "resize 扩充至9个元素后遍历 listInt_A:" << endl;
21 for (list<int>::iterator it = listInt_A.begin(); it != listInt_A.end(); it++)
22 {
23 cout << *it << " ";
24 }
25 cout << endl;
26
27 //使用 resize 重新指定容器长度
28 listInt_A.resize(3);
29 cout << "resize 删减至3个元素后遍历 listInt_A:" << endl;
30 for (list<int>::iterator it = listInt_A.begin(); it != listInt_A.end(); it++)
31 {
32 cout << *it << " ";
33 }
34 cout << endl;
35
36 return 0;
37 }

打印结果:

=====================================================================================================================

STL——容器(List)list 的大小操作的更多相关文章

  1. STL容器能力一览表和各个容器操作函数异常保证

    STL容器能力一览表 Vector Deque List Set Multiset map Multimap 典型内部 结构 dynamic array Array of arrays Doubly ...

  2. STL容器的适用情况

     转自http://hsw625728.blog.163.com/blog/static/3957072820091116114655254/ ly; mso-default-props:yes; m ...

  3. STL容器与配接器

    STL容器包括顺序容器.关联容器.无序关联容器 STL配接器包括容器配接器.函数配接器 顺序容器: vector                             行为类似于数组,但可以根据要求 ...

  4. STL容器的本质

    http://blog.sina.com.cn/s/blog_4d3a41f40100eof0.html 最近在学习unordered_map里面的散列函数和相等函数怎么写.学习过程中看到了一个好帖子 ...

  5. STL容器的内存分配

    这篇文章参考的是侯捷的<STL源码剖析>,所以主要介绍的是SGI STL实现版本,这个版本也是g++自带的版本,另外有J.Plauger实现版本对应的是cl自带的版本,他们都是基于HP实现 ...

  6. STL容器总结

    一. 种类: 标准STL序列容器:vector.string.deque和list. 标准STL关联容器:set.multiset.map和multimap. 非标准序列容器slist和rope.sl ...

  7. C++ STL中Map的相关排序操作:按Key排序和按Value排序 - 编程小径 - 博客频道 - CSDN.NET

    C++ STL中Map的相关排序操作:按Key排序和按Value排序 - 编程小径 - 博客频道 - CSDN.NET C++ STL中Map的相关排序操作:按Key排序和按Value排序 分类: C ...

  8. STL - 容器共性机制研究

    C++模板是容器的概念. 理论提高:所有容器提供的都是值(value)语意,而非引用(reference)语意.容器执行插入元素的操作时,内部实施拷贝动作.所以STL容器内存储的元素必须能够被拷贝(必 ...

  9. 关于STL容器

    容器: 概念:如果把数据看做物体,容器就是放置这些物体的器物,因为其内部结构不同,数据摆放的方式不同,取用的方式也不同,我们把他们抽象成不同的模板类,使用时去实例化它 分类: 序列容器.关联容器.容器 ...

  10. STL容器底层数据结构的实现

    C++ STL 的实现: 1.vector      底层数据结构为数组 ,支持快速随机访问   2.list            底层数据结构为双向链表,支持快速增删   3.deque     ...

随机推荐

  1. Spring第三天,详解Bean的生命周期,学会后让面试官无话可说!

    点击下方链接回顾往期 不要再说不会Spring了!Spring第一天,学会进大厂! Spring第二天,你必须知道容器注册组件的几种方式!学废它吊打面试官! 今天讲解Spring中Bean的生命周期. ...

  2. MySQL索引背后的数据结构及原理

    摘要 本文以MySQL数据库为研究对象,讨论与数据库索引相关的一些话题.特别需要说明的是,MySQL支持诸多存储引擎,而各种存储引擎对索引的支持也各不相同,因此MySQL数据库支持多种索引类型,如BT ...

  3. Android呼吸灯添加

    平台:mtk 一.hal层入口    Lights.c (vendor\mediatek\proprietary\hardware\liblights)     char const*const RE ...

  4. ceph 集群快速部署

    1.三台Centos7的主机 [root@ceph-1 ~]# cat /etc/redhat-release CentOS Linux release 7.2.1511 (Core)    2.主机 ...

  5. 基于Vue、Springboot网站实现第三方登录之QQ登录,以及邮件发送

    基于Vue.Springboot实现第三方登录之QQ登录 前言 一.前提(准备) 二.QQ登录实现 1.前端 2.后端 1.application.yml 和工具类QQHttpClient 2.QQL ...

  6. linux(centos7.x)安装jdk

    一.下载与安装 下载地址:链接:https://pan.baidu.com/s/1g7MF1xqlOxWnLGf2shl3NA   提取码:epae  下载完成后将安装包上传到linxu环境中,并将其 ...

  7. maven打包时报No compiler is provided in this environment处理

    系统:macOS 开发工具:Idea 问题描述:在idea中执行mvn clean install时报No compiler is provided in this environment. Perh ...

  8. 一枚程序猿的MacBook M1详细体验报告

    前言 2020年11月11日双十一上午,苹果发布了M1芯片的新款Mac,其最大的变化就是将处理器从Intel换成了苹果自研的ARM芯片M1. 上一次苹果更换Mac芯片要追溯到2006年,14年前,苹果 ...

  9. springboot多模块项目搭建遇到的问题记录

    废话不多说,直接上问题报错与解决方法. 问题报错一:(报错信息看下方代码) 问题原因:'com.company.logistics.service.company.CompanyService' 未找 ...

  10. Python中迭代循环使用比较多的range函数的作用

    range函数用于生成一个不可变的数字序列可迭代对象,类型为range,该数字序列通常用于在 for 循环中循环指定的次数. 具体可参考:<Python中与迭代相关的函数>的详细介绍 老猿 ...