STL——容器(List)list 的赋值操作
list.assign(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 };
9 list<int> listInt_A(num, num + size(num));
10 list<int> listInt_B;
11
12 cout << "遍历 listInt_A:";
13 for (list<int>::iterator it = listInt_A.begin(); it != listInt_A.end(); it++)
14 {
15 cout << *it << " ";
16 }
17 cout << endl;
18
19 listInt_B.assign(++listInt_A.begin(), --listInt_A.end());
20 cout << "遍历 listInt_B:";
21 for (list<int>::iterator it = listInt_B.begin(); it != listInt_B.end(); it++)
22 {
23 cout << *it << " ";
24 }
25 cout << endl;
26
27 return 0;
28 }
打印结果:
end()是结束符,但没有打印出来555,是因为前开后闭,

list.assign(n, elem);
//将n个elem拷贝赋值给本身
1 #include <iostream>
2 #include <list>
3
4 using namespace std;
5
6 int main()
7 {
8 list<int> listInt_A(5, 888);
9
10 cout << "遍历 listInt_A:";
11 for (list<int>::iterator it = listInt_A.begin(); it != listInt_A.end(); it++)
12 {
13 cout << *it << " ";
14 }
15 cout << endl;
16
17 return 0;
18 }
打印结果:

list& operator=(const list& lst);
//重载等号操作符
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 cout << "遍历 listInt_A:";
13 for (list<int>::iterator it = listInt_A.begin(); it != listInt_A.end(); it++)
14 {
15 cout << *it << " ";
16 }
17 cout << endl;
18
19 listInt_B = listInt_A;
20 cout << "遍历 listInt_B:";
21 for (list<int>::iterator it = listInt_B.begin(); it != listInt_B.end(); it++)
22 {
23 cout << *it << " ";
24 }
25 cout << endl;
26
27 return 0;
28 }
打印结果:

list.swap(lst);
//将lst与本身的元素互换
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(5, 888);
11
12 cout << "互换前 遍历 listInt_A:";
13 for (list<int>::iterator it = listInt_A.begin(); it != listInt_A.end(); it++)
14 {
15 cout << *it << " ";
16 }
17 cout << endl;
18 cout << "互换前 遍历 listInt_B:";
19 for (list<int>::iterator it = listInt_B.begin(); it != listInt_B.end(); it++)
20 {
21 cout << *it << " ";
22 }
23 cout << endl;
24
25 //互换
26 listInt_A.swap(listInt_B);
27 cout << "互换后 遍历 listInt_A:";
28 for (list<int>::iterator it = listInt_A.begin(); it != listInt_A.end(); it++)
29 {
30 cout << *it << " ";
31 }
32 cout << endl;
33 cout << "互换后 遍历 listInt_B:";
34 for (list<int>::iterator it = listInt_B.begin(); it != listInt_B.end(); it++)
35 {
36 cout << *it << " ";
37 }
38 cout << endl;
39
40 return 0;
41 }
打印结果:

===================================================================================================================
STL——容器(List)list 的赋值操作的更多相关文章
- STL容器能力一览表和各个容器操作函数异常保证
STL容器能力一览表 Vector Deque List Set Multiset map Multimap 典型内部 结构 dynamic array Array of arrays Doubly ...
- STL容器之一vector
STL中最简单也是最有用的容器之一是vector<T>类模板,称为向量容器,是序列类型容器中的一种. 1.vector<T> 对象的基本用法(1)声明:vector<ty ...
- STL容器 erase的使用陷井
http://www.cppblog.com/beautykingdom/archive/2008/07/09/55760.aspx?opt=admin 在STL(标准模板库)中经常会碰到要删除容器中 ...
- 史上最全的各种C++ STL容器全解析
史上最全的C++ STL 容器大礼包 为什么\(C++\)比\(C\)更受人欢迎呢?除了\(C++\) 的编译令人感到更舒适,\(C++\)的标准模板库(\(STL\))也占了很重要的原因.当你还在用 ...
- STL容器概述
STL容器 1.容器概述 1.1.容器分类 1.1.1.顺序容器:提供对元素序列的访问,顺序容器为元素连续分配内存或将元素组织为链表,元素的类型是容器成员value_type. 顺序容器 说明 vec ...
- STL——容器(List)List 的概念
1. List 容器的基本概念 1. list 是一个双向链表容器,可高效的进行插入删除元素,他的原理在于每个元素都有两个指针来记录前后两个元素的地址,像火车车厢一样,list 中各个元素在物理存储单 ...
- 【C++】STL容器
STL容器 标签:c++ 目录 STL容器 容器的成员函数 所有容器都有的 顺序容器和关联容器 顺序容器(vector/string/list/deque) 容器 vector 构造函数 操作 set ...
- 深入理解Javascript--作用域和赋值操作
作用域作为一个最基础的功能存在于各种编程语言中,它使得我们的编程更加灵活有趣.其基础功能就是存储变量中的值,然后可以对值进行访问和修改. 可能我们都知道作用域的一些概念,以及其一些扩展的一些内容闭包等 ...
- STL容器
啦啦啦,今天听啦高年级学长讲的STL容器啦,发现有好多东西还是有必要记载的,毕竟学长是身经百战的,他在参加各种比赛的时候积累的经验可不是一天两天就能学来的,那个可是炒鸡有价值的啊,啊啊啊啊啊 #inc ...
- c++ stl容器set成员函数介绍及set集合插入,遍历等用法举例
c++ stl集合set介绍 c++ stl集合(Set)是一种包含已排序对象的关联容器.set/multiset会根据待定的排序准则,自动将元素排序.两者不同在于前者不允许元素重复,而后者允许. 1 ...
随机推荐
- 每天一个linux命令之stat
[Linux]Linux下使用stat命令所显示出来的三个时间 转 https://blog.csdn.net/pointer_y/article/details/54347968 在linux系统下 ...
- 关于steam平台“wallpaper engine”软件出现界面黑屏,但壁纸能播放的问题
前阵子重装电脑后,在使用wallpaper engine这款软件时发现了以下令人疑惑的画面: 点击"设置"和"壁纸选择"界面全是黑的......这还没完,更气人 ...
- 在FL Studio中如何制作和优化你的人声和弦(Vocal Chords)
人声和弦在Future Bass.Melodic Dubstep等类型的电子音乐中被常用.与一般的和弦相同,其主要起到为主旋律做铺垫的效果,但是人声和弦加入了人声的因素,可以使得和弦更有趣,更有电子音 ...
- FL Studio 插件使用教程 —— 3x Osc(下)
我们继续深入研究一下fl的3x Osc教程. 包络线是修饰音色非常重要的一个部件,有了它,音色不再是单调的长音,而能有长有短,有深有浅,变得丰富多彩.因此,学习包络线的运作原理很重要. 图1:包络线界 ...
- 「LOJ 3153」 「JOI Open 2019」三级跳
题面 LOJ 3153 solution 对于任意一对\(A,B\),若区间\([A,B]\)中存在一个数权值大于\(A\)或\(B\),则用这个数来替代\(A\)或\(B\)显然更优. 故只需要考虑 ...
- css3系列之@font-face
@font-face 这个属性呢,必须设置在 css的根下,也就是说,不能设置在任何元素里面. @font-face: 参数: font-family: 给这个文字库 起个名字. src: url( ...
- MySQL必知必会:简介undo log、truncate、以及undo log如何帮你回滚事物
目录 一.前言 二.undo log表空间 三.关于undo log默认的配置 四.如何将undo log放到单独的表空间 文章公众号首发,持续更新中 五.rollback segment 六.什么是 ...
- 2. 三数之和(数组、hashset)
思路及算法: 该题与第一题的"两数之和"相似,三数之和为0,不就是两数之和为第三个数的相反数吗?因为不能重复,所以,首先进行了一遍排序:其次,在枚举的时候判断了本次的第三个数的值是 ...
- Java多线程中的wait/notify通信模式
前言 最近在看一些JUC下的源码,更加意识到想要学好Java多线程,基础是关键,比如想要学好ReentranLock源码,就得掌握好AQS源码,而AQS源码中又有很多Java多线程经典的一些应用:再比 ...
- 你的Idea还可用吗?不妨试试这个神器!
@ 目录 一.STS安装 1.STS下载 2.STS安装 二.STS使用 1.STS配置JDK 2.STS配置Maven 3.使用STS创建SpringBoot项目 三.优化STS 1.主题美化 2. ...