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 的赋值操作的更多相关文章

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

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

  2. STL容器之一vector

    STL中最简单也是最有用的容器之一是vector<T>类模板,称为向量容器,是序列类型容器中的一种. 1.vector<T> 对象的基本用法(1)声明:vector<ty ...

  3. STL容器 erase的使用陷井

    http://www.cppblog.com/beautykingdom/archive/2008/07/09/55760.aspx?opt=admin 在STL(标准模板库)中经常会碰到要删除容器中 ...

  4. 史上最全的各种C++ STL容器全解析

    史上最全的C++ STL 容器大礼包 为什么\(C++\)比\(C\)更受人欢迎呢?除了\(C++\) 的编译令人感到更舒适,\(C++\)的标准模板库(\(STL\))也占了很重要的原因.当你还在用 ...

  5. STL容器概述

    STL容器 1.容器概述 1.1.容器分类 1.1.1.顺序容器:提供对元素序列的访问,顺序容器为元素连续分配内存或将元素组织为链表,元素的类型是容器成员value_type. 顺序容器 说明 vec ...

  6. STL——容器(List)List 的概念

    1. List 容器的基本概念 1. list 是一个双向链表容器,可高效的进行插入删除元素,他的原理在于每个元素都有两个指针来记录前后两个元素的地址,像火车车厢一样,list 中各个元素在物理存储单 ...

  7. 【C++】STL容器

    STL容器 标签:c++ 目录 STL容器 容器的成员函数 所有容器都有的 顺序容器和关联容器 顺序容器(vector/string/list/deque) 容器 vector 构造函数 操作 set ...

  8. 深入理解Javascript--作用域和赋值操作

    作用域作为一个最基础的功能存在于各种编程语言中,它使得我们的编程更加灵活有趣.其基础功能就是存储变量中的值,然后可以对值进行访问和修改. 可能我们都知道作用域的一些概念,以及其一些扩展的一些内容闭包等 ...

  9. STL容器

    啦啦啦,今天听啦高年级学长讲的STL容器啦,发现有好多东西还是有必要记载的,毕竟学长是身经百战的,他在参加各种比赛的时候积累的经验可不是一天两天就能学来的,那个可是炒鸡有价值的啊,啊啊啊啊啊 #inc ...

  10. c++ stl容器set成员函数介绍及set集合插入,遍历等用法举例

    c++ stl集合set介绍 c++ stl集合(Set)是一种包含已排序对象的关联容器.set/multiset会根据待定的排序准则,自动将元素排序.两者不同在于前者不允许元素重复,而后者允许. 1 ...

随机推荐

  1. 分布式监控系统之Zabbix基础使用

    前文我们了解了分布式监控系统zabbix的相关组件的作用和zabbix的部署,回顾请参考https://www.cnblogs.com/qiuhom-1874/p/13997582.html:今天我们 ...

  2. 有关线上系统点击没有任何相应得问题思考,主要针对PC端应用程序

    1.问题得起因 前段时间,客户得某些机器上,点击应用系统得快捷方式,没有任何响应,不弹出程序主界面,也没有任何得报错提示,甚至程序得错误日志也没有任何输出. 当时,听说发生这种情况得时候,有点懵了,不 ...

  3. Qt QPainter画个球啊

    Qt QPainter画个球啊 目录 Qt QPainter画个球啊 看效果 方法 代码 看效果 方法 使用绘图事件,绘制一个图形 使用定时事件,不停更新图形位置 代码 .h #pragma once ...

  4. 精尽MyBatis源码分析 - MyBatis初始化(二)之加载Mapper接口与XML映射文件

    该系列文档是本人在学习 Mybatis 的源码过程中总结下来的,可能对读者不太友好,请结合我的源码注释(Mybatis源码分析 GitHub 地址.Mybatis-Spring 源码分析 GitHub ...

  5. [原题复现]BJDCTF2020 WEB部分全部解

    简介  原题复现:https://gitee.com/xiaohua1998/BJDCTF2020_January  线上平台:https://buuoj.cn(北京联合大学公开的CTF平台) 榆林学 ...

  6. CVE-2020-1938复现

    一.漏洞描述        Tomcat是Apache开源组织开发的用于处理HTTP服务的项目,两者都是免费的,都可以做为独立的Web服务器运行.Apache Tomcat服务器存在文件包含漏洞,攻击 ...

  7. 探究:nuget工具对不再使用的dll文件的处理策略

    背景介绍 nuget是.net平台有效的包管理工具,相信每个C#开发者对它都不陌生. 本文我们来探究一下nuget对不再使用的dll文件的处理策略,分为如下2个场景: 场景A:包A1.0原来包含New ...

  8. Xrepo:一个现代化的跨平台 C/C++ 包管理器

    xrepo 是一个基于 Xmake 的跨平台 C/C++ 包管理器. 项目源码 官方文档 它基于 xmake 提供的运行时,但却是一个完整独立的包管理程序,相比 vcpkg/homebrew 此类包管 ...

  9. Guitar Pro指弹入门——特殊拍号

    在吉他演奏技术不断提高的同时,我们经常会遇到一些奇怪的曲谱.他们的拍号不是正常的4/4拍或者3/4拍,而是5/4或者5/8等等我们不太了解的拍号,致使我们在演奏和练习之中陷入纷乱的节奏. 那么本期文章 ...

  10. Sonar检测Math.abs(new Random().nextInt()) “Use the original value instead”

    今天早上旁边同事喊我看一个Sonar检测出的问题: 当时看了好几眼没觉得这个有太大问题,于是又看了下Sonar建议: 这是说Math.abs()方法使用在数字上面可能返回最小值,觉得这个挺有意思的,于 ...