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. 如何替换Ceph的Journal

    很多人会提出这样的问题: 能不能够将 Ceph journal 分区从一个磁盘替换到另一个磁盘? 怎样替换 Ceph 的 journal 分区? 有两种方法来修改Ceph的journal: 创建一个j ...

  2. 「NOIP2016」天天爱跑步 题解

    (声明:图片来源于网络) 「NOIP2016」天天爱跑步 题解 题目TP门 题目 题目描述 小c同学认为跑步非常有趣,于是决定制作一款叫做<天天爱跑步>的游戏.<天天爱跑步>是 ...

  3. 深度分析:Java虚拟机类加载机制、过程与类加载器

    虚拟机类加载机制是把描述类的数据从 Class 文件加载到内存,并对数据进行校验.转换解析和初始化,最终形成可以被虚拟机直接使用的 Java 类型. ​ 需要注意的是 Java 语言与其他编译时需要进 ...

  4. 又陷入知识盲区了,面试被问SpringBoot集成dubbo,我当时就懵了

    前言 前两天在和粉丝聊天的时候,粉丝跟我说之前在面试的时候被问到SpringBoot这一块的知识被问的有点懵,和我问了不少这方面的东西.事后我想了想不如把这些东西分享出来吧,让更多的人看到,这样不管是 ...

  5. Word1-提取图片文字

    1.OneNote # 将图片以图片格式粘贴在OneNote中-右键选择"复制图片中的文本"-粘贴"只保留文本"即可 这种方式识别率较高!!! 2.手机QQ图片 ...

  6. web自动化 模拟鼠标、键盘操作

    一.鼠标操作 1.1鼠标的悬停操作,move_to_element from selenium import webdriver from selenium.webdriver.common.acti ...

  7. 【VUE】8.VUEX核心概念

    1. Vuex核心概念主要如下 state : 存储共享数据 mutation: 变更store中的数据,方法,不能异步操作 action: 异步操作,通过触发mutation变更数据 getter: ...

  8. Unable to locate package python3 错误解决办法

    错误 huny@DESKTOP-N1EBKQP:/mnt/c/Users/Administrator$ sudo apt-get install python3 Reading package lis ...

  9. 【坑爹的mybtis plus】wrapper.in击垮了我们的数据库!

    mybatis plus让我们从很大程度上实现了用"java去写sql",但是有些很隐晦的使用方式,如果不注意的话,也会引起错误: 如果.in的时候给了一个null,这个时候并不会 ...

  10. 【mq读书笔记】mq读写分离机制

    mq根据brokerName查找Broker地址的过程 mq根据MessageQueue查找Broker地址的唯一依据是brokerName,同一组Broker(M-S)他们的bokerName相同但 ...