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 ...
随机推荐
- Java学习之AWT GUI编程
Java学习之AWT GUI编程 0x00 前言 既然前面提到了要重写冰蝎和一些反序列化工具,当然就不能随便说说而已.在编写这些工具还是要使用图形化工具来的方便一些,所以提前把GUI的框架给学习一遍. ...
- 图创图书管理SQL注入漏洞
payload root@kali:~# sqlmap -u http://2*0.*6.***.3:8080/opac/recommend/recommendBookList/list --data ...
- 安恒DASCTF 四月战 WP
web1 打开提就是源码审计 考点:反序列化POP链.反序列化字符串逃逸 show_source("index.php"); function write($data) { ret ...
- SpringSecurity了解
在web开发中,安全第一位!!过滤器.拦截器~ 属于非功能性需求. 做网站:安全应该在什么时候考虑?设计之初!! 漏洞,隐私泄露~ 假设架构一旦确定~ shiro和SpringSecurity的区别: ...
- MathType颜色设置的技巧
MathType功能非常强大,在编辑公式时使用非常方便.运用MathType不仅可以改变公式的字体和字号,也可以改变公式字体颜色,MathType颜色设置还是有一套技术的,下面我们就一起来看看公式编辑 ...
- Camtasia处理音频制作BGM
我们在剪辑自己录制的视频时经常会需要同时对音频进行处理,camtasia是一个可以对视频的音频或者单独的音频文件进行处理的软件,那我们就来看看到底如何使用camtasia 2019(Win)来处理音频 ...
- Golang 实现 Redis(7): Redis 集群与一致性 Hash
本文是使用 golang 实现 redis 系列的第七篇, 将介绍如何将单点的缓存服务器扩展为分布式缓存.godis 集群的源码在Github:Godis/cluster 单台服务器的CPU和内存等资 ...
- C语言新手入门
include<stdio.h> int main() { //输入一个数 输出它的反序列 c int a,b=0; scanf("%d",&a);//输入一个 ...
- 领域设计:Entity与VO
本文探讨如下内容: 什么是状态 什么是标识 什么是Entity 什么是VO(ValueObject) 在设计中如何识别Entity和VO 要理解Entity和VO,需要先理解两个概念:「状态」和「标识 ...
- windows中flask的环境搭建
之前在ctf中遇到了python模板注入的题,于是就打算学习一下flask框架,它是基于Python的Web轻量级应用框架,与其他框架相比,Flask可以自主选择应用组件,可扩展性强. 安装也简单 第 ...