push_back(elem);

//在容器尾部加入一个元素

 1 #include <iostream>
2 #include <list>
3
4 using namespace std;
5
6 int main()
7 {
8 int num[] = { 111,222,333 };
9 list<int> listInt(num, num + size(num));
10 cout << "初始遍历 listInt:";
11 for (list<int>::iterator it = listInt.begin(); it != listInt.end(); it++)
12 {
13 cout << *it << " ";
14 }
15 cout << endl;
16
17 listInt.push_back(444);
18 cout << "push_back后遍历 listInt:";
19 for (list<int>::iterator it = listInt.begin(); it != listInt.end(); it++)
20 {
21 cout << *it << " ";
22 }
23 cout << endl;
24
25 return 0;
26 }

打印结果:

pop_back();

//删除容器中最后一个元素

 1 #include <iostream>
2 #include <list>
3
4 using namespace std;
5
6 int main()
7 {
8 int num[] = { 111,222,333 };
9 list<int> listInt(num, num + size(num));
10 cout << "初始遍历 listInt:";
11 for (list<int>::iterator it = listInt.begin(); it != listInt.end(); it++)
12 {
13 cout << *it << " ";
14 }
15 cout << endl;
16
17 listInt.pop_back();
18 cout << "pop_back 后遍历 listInt:";
19 for (list<int>::iterator it = listInt.begin(); it != listInt.end(); it++)
20 {
21 cout << *it << " ";
22 }
23 cout << endl;
24
25 return 0;
26 }

打印结果:

push_front(elem);

//在容器开头插入一个元素

 1 #include <iostream>
2 #include <list>
3
4 using namespace std;
5
6 int main()
7 {
8 int num[] = { 111,222,333 };
9 list<int> listInt(num, num + size(num));
10 cout << "初始遍历 listInt:";
11 for (list<int>::iterator it = listInt.begin(); it != listInt.end(); it++)
12 {
13 cout << *it << " ";
14 }
15 cout << endl;
16
17 listInt.push_front(0);
18 cout << "push_front 后遍历 listInt:";
19 for (list<int>::iterator it = listInt.begin(); it != listInt.end(); it++)
20 {
21 cout << *it << " ";
22 }
23 cout << endl;
24
25 return 0;
26 }

打印结果:

pop_front();

//从容器开头移除第一个元素

 1 #include <iostream>
2 #include <list>
3
4 using namespace std;
5
6 int main()
7 {
8 int num[] = { 111,222,333 };
9 list<int> listInt(num, num + size(num));
10 cout << "初始遍历 listInt:";
11 for (list<int>::iterator it = listInt.begin(); it != listInt.end(); it++)
12 {
13 cout << *it << " ";
14 }
15 cout << endl;
16
17 listInt.pop_front();
18 cout << "pop_front 后遍历 listInt:";
19 for (list<int>::iterator it = listInt.begin(); it != listInt.end(); it++)
20 {
21 cout << *it << " ";
22 }
23 cout << endl;
24
25 return 0;
26 }

打印结果:

insert(pos, elem);

//在pos位置插elem元素的拷贝,返回新数据的位置

这里需要注意一点,list 不可以随机存取元素,所以不支持 at.(position)函数与[]操作符。可以对其迭代器执行++和--,但是不能这样操作迭代器:it + 3

 1 #include <iostream>
2 #include <list>
3
4 using namespace std;
5
6 int main()
7 {
8 int num[] = { 111,222,333 };
9 list<int> listInt(num, num + size(num));
10 cout << "初始遍历 listInt:";
11 for (list<int>::iterator it = listInt.begin(); it != listInt.end(); it++)
12 {
13 cout << *it << " ";
14 }
15 cout << endl;
16
17
18 cout << "insert 后,用 insert 的返回值遍历 listInt:";
19 for (list<int>::iterator it = listInt.insert(++listInt.begin(), 888); it != listInt.end(); it++)
20 {
21 cout << *it << " ";
22 }
23 cout << endl;
24 cout << "最终遍历 listInt:";
25 for (list<int>::iterator it = listInt.begin(); it != listInt.end(); it++)
26 {
27 cout << *it << " ";
28 }
29
30 return 0;
31 }

打印结果:

insert(pos, n, elem);

//在pos位置插入n个elem数据,返回新数据的第一个数据的位置(这个有没有返回值是编译器版本决定,早起版本的编译器没有返回值)

这里需要注意一点,list 不可以随机存取元素,所以不支持 at.(position)函数与[]操作符。可以对其迭代器执行++和--,但是不能这样操作迭代器:it + 3

 1 #include <iostream>
2 #include <list>
3
4 using namespace std;
5
6 int main()
7 {
8 int num[] = { 111,222,333 };
9 list<int> listInt(num, num + size(num));
10 cout << "初始遍历 listInt:";
11 for (list<int>::iterator it = listInt.begin(); it != listInt.end(); it++)
12 {
13 cout << *it << " ";
14 }
15 cout << endl;
16
17
18 cout << "insert 后,用 insert 的返回值遍历 listInt:";
19 for (list<int>::iterator it = listInt.insert(++listInt.begin(), 2, 888); it != listInt.end(); it++)
20 {
21 cout << *it << " ";
22 }
23 cout << endl;
24 cout << "最终遍历 listInt:";
25 for (list<int>::iterator it = listInt.begin(); it != listInt.end(); it++)
26 {
27 cout << *it << " ";
28 }
29
30 return 0;
31 }

打印结果:

insert(pos, beg, end);

//在pos位置插入[beg,end)区间的数据,下面代码我举两种使用方法,一种是使用迭代器插入,另一种是插入数组

这里需要注意一点,list 不可以随机存取元素,所以不支持 at.(position)函数与[]操作符。可以对其迭代器执行++和--,但是不能这样操作迭代器:it + 3

 1 #include <iostream>
2 #include <list>
3
4 using namespace std;
5
6 int main()
7 {
8 int num[] = { 111,222,333 };
9 int num_1[] = { 666,777,888 };
10 list<int> listInt(num, num + size(num));
11 list<int> listInt_A(2, 666);
12 cout << "初始遍历 listInt:";
13 for (list<int>::iterator it = listInt.begin(); it != listInt.end(); it++)
14 {
15 cout << *it << " ";
16 }
17 cout << endl;
18
19
20 cout << "insert 后,用 insert 的返回值遍历 listInt:";
21 for (list<int>::iterator it = listInt.insert(++listInt.begin(), listInt_A.begin(), listInt_A.end()); it != listInt.end(); it++)
22 {
23 cout << *it << " ";
24 }
25
26 cout << endl;
27 cout << "最终遍历 listInt:";
28 for (list<int>::iterator it = listInt.begin(); it != listInt.end(); it++)
29 {
30 cout << *it << " ";
31 }
32 cout << endl << endl;
33
34
35 //当然这里也可以插入数组
36 cout << "使用 insert 插入数组,然后用 insert 的返回值遍历 listInt:" << endl;
37 for (list<int>::iterator it = listInt.insert(++listInt.begin(), num_1, num_1 + size(num_1)); it != listInt.end(); it++)
38 {
39 cout << *it << " ";
40 }
41
42 cout << endl;
43 cout << "最终遍历 listInt:";
44 for (list<int>::iterator it = listInt.begin(); it != listInt.end(); it++)
45 {
46 cout << *it << " ";
47 }
48
49 return 0;
50 }

打印结果:

clear();

//移除容器的所有数据

 1 #include <iostream>
2 #include <list>
3
4 using namespace std;
5
6 int main()
7 {
8 int num[] = { 111,222,333 };
9 list<int> listInt(num, num + size(num));
10 cout << "clear 前遍历 listInt:";
11 for (list<int>::iterator it = listInt.begin(); it != listInt.end(); it++)
12 {
13 cout << *it << " ";
14 }
15 cout << endl;
16 cout << "clear 前 listInt.size() = " << listInt.size() << endl;
17
18
19 listInt.clear();
20 cout << "clear 后遍历 listInt:";
21 for (list<int>::iterator it = listInt.begin(); it != listInt.end(); it++)
22 {
23 cout << *it << " ";
24 }
25 cout << endl;
26 cout << "clear 后 listInt.size() = " << listInt.size() << endl;
27
28 return 0;
29 }

打印结果:

erase(beg, end);

//删除[beg,end)区间的数据,返回下一个数据的位置

 1 #include <iostream>
2 #include <list>
3
4 using namespace std;
5
6 int main()
7 {
8 int num[] = { 111,222,333,444,555,666 };
9 list<int> listInt(num, num + size(num));
10 cout << "erase 前遍历 listInt:";
11 for (list<int>::iterator it = listInt.begin(); it != listInt.end(); it++)
12 {
13 cout << *it << " ";
14 }
15 cout << endl;
16
17
18 cout << "erase 后,用其返回值遍历 listInt:";
19 for (list<int>::iterator it = listInt.erase(++listInt.begin(), --listInt.end()); it != listInt.end(); it++)
20 {
21 cout << *it << " ";
22 }
23
24 cout << endl;
25 cout << "erase 后遍历 listInt:";
26 for (list<int>::iterator it = listInt.begin(); it != listInt.end(); it++)
27 {
28 cout << *it << " ";
29 }
30
31 return 0;
32 }

打印结果:

erase(pos);

//删除pos位置的数据,返回下一个数据的位置

 1 #include <iostream>
2 #include <list>
3
4 using namespace std;
5
6 int main()
7 {
8 int num[] = { 111,222,333,444,555,666 };
9 list<int> listInt(num, num + size(num));
10 cout << "erase 前遍历 listInt:";
11 for (list<int>::iterator it = listInt.begin(); it != listInt.end(); it++)
12 {
13 cout << *it << " ";
14 }
15 cout << endl;
16
17
18 cout << "erase 后,用其返回值遍历 listInt:";
19 for (list<int>::iterator it = listInt.erase(++listInt.begin()); it != listInt.end(); it++)
20 {
21 cout << *it << " ";
22 }
23
24 cout << endl;
25 cout << "erase 后遍历 listInt:";
26 for (list<int>::iterator it = listInt.begin(); it != listInt.end(); it++)
27 {
28 cout << *it << " ";
29 }
30
31 return 0;
32 }

打印结果:

remove(elem);

//删除容器中所有与elem值匹配的元素

 1 #include <iostream>
2 #include <list>
3
4 using namespace std;
5
6 int main()
7 {
8 int num[] = { 111,222,111,333,111,444 };
9 list<int> listInt(num, num + size(num));
10 cout << "erase 前遍历 listInt:";
11 for (list<int>::iterator it = listInt.begin(); it != listInt.end(); it++)
12 {
13 cout << *it << " ";
14 }
15 cout << endl;
16
17 listInt.remove(111);
18 cout << "remove 后遍历 listInt:";
19 for (list<int>::iterator it = listInt.begin(); it != listInt.end(); it++)
20 {
21 cout << *it << " ";
22 }
23
24 return 0;
25 }

打印结果:

还有一种遍历删除法:

 1 #include <iostream>
2 #include <list>
3
4 using namespace std;
5
6 int main()
7 {
8 int num[] = { 111,222,111,333,111,444 };
9 list<int> listInt(num, num + size(num));
10 cout << "erase 前遍历 listInt:";
11 for (list<int>::iterator it = listInt.begin(); it != listInt.end(); it++)
12 {
13 cout << *it << " ";
14 }
15 cout << endl;
16
17 for (list<int>::iterator it = listInt.begin(); it != listInt.end();)
18 {
19 if (*it == 111)
20 {
21 it = listInt.erase(it);
22 }
23 else
24 {
25 it++;
26 }
27 }
28
29 cout << "erase 后遍历 listInt:";
30 for (list<int>::iterator it = listInt.begin(); it != listInt.end(); it++)
31 {
32 cout << *it << " ";
33 }
34
35 return 0;
36 }

打印结果:

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

STL——容器(List)List 的数据元素插入和删除操作的更多相关文章

  1. [剑指offer]09用两个栈实现队列插入和删除操作,C++实现

    原创博文,转载请注明出处! # 本文为牛客网<剑指offer>刷题笔记 1.题目 # 用两个栈实现队列的插入和删除操作 2.思路 栈服从先入后出的原则处理数据,队列服从先入先出的原则处理数 ...

  2. Java创建二叉搜索树,实现搜索,插入,删除操作

    Java实现的二叉搜索树,并实现对该树的搜索,插入,删除操作(合并删除,复制删除) 首先我们要有一个编码的思路,大致如下: 1.查找:根据二叉搜索树的数据特点,我们可以根据节点的值得比较来实现查找,查 ...

  3. STL——容器(Map & multimap)的插入与迭代器

    1. 容器(Map & multimap)的插入 map.insert(...);    //往容器插入元素,返回pair<iterator,bool> map中插入元素的四种方式 ...

  4. jquery元素插入、删除、清空

    1)jquery元素插入 <!--位置1--> <div id='test'> <!--位置2--> <div>测试</div> <! ...

  5. jquery元素插入、删除、清空、找父子级元素

    1)jquery元素插入 <!--位置1--> <div id='test'> <!--位置2--> <div>测试</div> <! ...

  6. STL—— 容器(vector)数据插入insert()方法 的返回值

    vector 容器下的 insert() 方法拥有返回值,由于insert() 方法拥有4种重载函数,他的返回值不尽相同. 第一种,插入单个元素后的返回值: 1 #include <iostre ...

  7. STL——容器(deque)deque 的插入 insert()

    deque.insert(pos,elem); //在pos位置插入一个elem元素的拷贝,返回新数据的位置. 1 #include <iostream> 2 #include <d ...

  8. Java BLOB 数据的插入与读取 操作

    package com.lw.database; import java.io.FileInputStream; import java.io.FileOutputStream; import jav ...

  9. Thinkphp 数据的修改及删除操作

    一.数据修改操作 save()  实现数据修改,返回受影响的记录条数 具体有两种方式实现数据修改,与添加类似(数组.AR方式) 1.数组方式: a)         $goods = D(“Goods ...

随机推荐

  1. linux中KVM桥接网卡br0

    在centos虚拟化当中需要增加一个桥接网卡,然后将虚拟化当中的机器的网卡桥接到桥接网卡,下面将描述设置方法: 查看现有网卡 [root@zb ~]# vim /etc/sysconfig/netwo ...

  2. 新鲜出炉!春招-面试-阿里钉钉、头条广告,美团面经分享,看我如何拿下offer!

    之前给大家分享了一个朋友在字节面试的面试经历和拿到offer的过程,过程也算是比较精彩了,感兴趣的朋友可以去翻翻之前的那篇文章.话不多说重点来啦,一直有人发私信问我有没有其他大厂的面经分享啊,我也是联 ...

  3. 如何使用iMindMap制作更专业的时间计划

    时间计划无论是在日常生活中,还是在工作中,都显得极为重要.小到每周的购物时间规划,大到大型项目的时间管理,时间计划都会如影随形.虽然时间计划很重要,但很多人都会忽视这种重要性,可能只会在台本日历上作一 ...

  4. guitar pro系列教程(二十三):如何使用Guitar Pro制作扫弦

    前面的章节小编和大家讲解了很多关于Guitar Pro的使用功能,本章节我们将还是采用图文结合的方式和大家讲解如何使用Guitar Pro 制作扫弦,感兴趣的朋友可以进来看看哦. 扫弦的概念 对于很多 ...

  5. FL studio系列教程(十四):如何在FL Studio播放列表中排列样式

    我们在FL Studio中做好了节奏样式后就可以在播放列表窗口中进行乐曲的编排了.刚接触这款软件的同学肯定会对如何编排比较陌生但也比较憧憬的,因为它是从一个窗口到另一个窗口中的操作.其实明白了这里的知 ...

  6. websocket服务端开发

    基于http请求以拉的方式去做服务器的推送,无论是实时性和有效字节都是差强人意的效果. 公司的im系统在与客户端的交互上实际上借助了websocket来实现服务器与客户端的事实消息推送,今天就来简单了 ...

  7. mq checkpoint文件

    记录comitlog,consumeQueue,Index文件的刷盘时间点,文件固定长度4k,其中只用该文件的24个字节,其存储格式: 8字节physicMsgtimestamp+8字节logicsM ...

  8. Python爬虫合集:花6k学习爬虫,终于知道爬虫能干嘛了

    爬虫Ⅰ:爬虫的基础知识 爬虫的基础知识使用实例.应用技巧.基本知识点总结和需要注意事项 爬虫初始: 爬虫: + Request + Scrapy 数据分析+机器学习 + numpy,pandas,ma ...

  9. Fiddler 4的安装

    1.双击FiddlerSetup_2.0.20194.413.exe安装包 2点击同意 3.傻瓜式安装即可 4.安装完就是这个样子 5.然后点这个 然后不管出现什么都点yes(或者是),最后点击ok ...

  10. [Windows] Prism 8.0 入门(上):Prism.Core

    1. Prism 简介 Prism 是一个用于构建松耦合.可维护和可测试的 XAML 应用的框架,它支持所有还活着的基于 XAML 的平台,包括 WPF.Xamarin Forms.WinUI 和 U ...