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. PDF技术 -Java实现Html转PDF文件

    转载:https://blog.csdn.net/qq_34190023/article/details/82999702 html转换为pdf的关键技术是如何处理网页中复杂的css样式.以及中文乱码 ...

  2. git设置个人信息

    git config --global user.name "username" 设置下自己提交的用户名 git config --global user.email " ...

  3. 不会吧!做了这么久开发还有不会NIO的,看看BAT大佬是怎么用的吧

    前言 在将NIO之前,我们必须要了解一下Java的IO部分知识. BIO(Blocking IO) 阻塞IO,在Java中主要就是通过ServerSocket.accept()实现的. NIO(Non ...

  4. 给PDF文件创建书签,实现快速导航

    当文档中的页码比较多的情况下,使用目录进行导航是一个很好用的方法,为文档内容制作目录,方便快速查找目标内容.除了内容的快速导航,书签还能指明不同书签的层级关系,展现文档的结构. 图1:书签的功能 一. ...

  5. 简单几步就能把素材变成大片?老司机推荐Vegas

    "素材编辑"一般分为两种,一种是对时间线素材长度和位置的编辑,另一种就是遮罩法操作. 第一种,裁剪素材(将素材在我们选定的位置一分为二),对时间线上的素材进行裁剪,有两种方法: 一 ...

  6. FL Studio中如何使用插件混杂功能中的琶音器

    琶音指一串和弦音从低到高或从高到低依次连续奏出,可视为分解和弦的一种.通常作为一种专门的技巧训练用于练习曲中,有时作为短小的连接句或经过句出现在乐曲旋律声部中.在Trance类型电子音乐中,琶音的运用 ...

  7. Camtasia Studio,一款好用到爆炸的视频录制软件

    目前网络中有很多视频录制软件,各有特色,经过亲测今天小编为大家推荐一款比较好的视频录制软件就是Camtasia Studio软件,Camtasia支持视频的高清录制以及视频的导入导出,而且最支持导出的 ...

  8. 学Python,只有不到15%的同学会成功

    我给大家唱首歌:<坚持的意义> 你看过了许多书籍 你看过了许多视频 你迷失在屏幕上每一道短暂的光阴 你品尝了代码的糟心 你踏过算法的荆棘 你熟记书本里每一段你最爱的公式 却说不出你爱Pyt ...

  9. String.Split()函数 非原创

    我们在上次学习到了 String.Join函数(http://blog.csdn.net/zhvsby/archive/2008/11/28/3404704.aspx),其中用到了String.SPl ...

  10. SkyWalking —— 分布式应用监控与链路追踪

    SkyWalking 是一个应用性能监控系统,特别为微服务.云原生和基于容器(Docker, Kubernetes, Mesos)体系结构而设计.除了应用指标监控以外,它还能对分布式调用链路进行追踪. ...