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 ...
随机推荐
- ubuntu服务器dns重启失效问题
方法一 通过/etc/network/interfaces,在它的最后增加一句: dns-nameservers 8.8.8.8 8.8.8.8是Google提供的DNS服务,这里只是举一个例子,你也 ...
- 对App应用架构搭建的一些思考
当下随着App开发技术的越来越成熟,多人协同开发必不可少,一个团队中每个人的代码风格.技术栈都存在差异,因此统一一套成熟的开发架构必不可少,可以提高开发效率.统一代码风格.为项目维护提供便利. 当下A ...
- 2018.1.15复习_ css+js
[1]几个常见的css标签:--------------------------------------------------background-color; 设置背景颜色background-p ...
- 我要进大厂之大数据MapReduce知识点(1)
01 我们一起学大数据 老刘今天分享的是大数据Hadoop框架中的分布式计算MapReduce模块,MapReduce知识点有很多,大家需要耐心看,用心记,这次先分享出MapReduce的第一部分.老 ...
- vim进入粘贴模式
最近使用linux的vim编辑器编写程序时,遇到一些繁琐的模板想要复制粘贴进去,直接进入插入模式点复制,复制出来的格式不对没办法运行 解决办法: 这是因为 Vim 自动缩进了,按照如下设置可以解决该问 ...
- 在iOS中使用ZBar扫描二维码和条形码
最近做了个外包项目,里面用到了二维码扫描和微信支付!之前比较熟悉的是ZXing,但是在Xcode7.1里面发现竟然莫名的不支持,木有办法,从网上查了一下还有一种支持二维码扫描的东西,没错就是接下来我要 ...
- python办公入门3:xlrd操作工作表
工作表 1 import xlrd 2 3 #接受工作表 4 data=xlrd.open_workbook("data.xlsx") 5 #查询第一个工作表的打开状态 6 pri ...
- linux设置共享文件夹 - samba
安装samba sudo apt-get install samba 配置 /etc/samba/smb.conf 的global模块添加security = user 最下加入 [share] pa ...
- 在windows环境下 nginx + .net core 3.1 实现反向代理和负载均衡
一.创建.net core web 应用 1.首先打开vs2019创建好.net core web应用,简单的注入IConfiguration 便于打印端口号展示效果. 1 private reado ...
- CentOS下配置VNC
配置桌面 # 安装gnome桌面环境 yum groupinstall Desktop -y # 安装中文语言支持包(可选) yum groupinstall 'Chinese Support' -y ...