STL——容器(deque) deque 的赋值 assign() operator=() swap()
deque 的赋值分下边4种方法:
deque.assign(beg,end);
//将[beg, end)区间中的数据拷贝赋值给本身。注意该区间是左闭右开的区间。
1 #include <iostream>
2 #include <deque>
3
4 using namespace std;
5
6 int main()
7 {
8 deque<int> deqInt_A, deqInt_B;
9
10 deqInt_A.push_back(1);
11 deqInt_A.push_back(2);
12 deqInt_A.push_back(3);
13 deqInt_A.push_back(4);
14
15 deqInt_B.assign(deqInt_A.begin(), deqInt_A.end());
16
17 cout << "deqInt_B 中的元素为:" << endl;
18 for (deque<int>::iterator it = deqInt_B.begin(); it != deqInt_B.end(); it++)
19 {
20 cout << *it << " ";
21 }
22
23 return 0;
24 }
打印结果:
deque.assign(n,elem);
//将n个elem拷贝赋值给本身。
1 #include <iostream>
2 #include <deque>
3
4 using namespace std;
5
6 int main()
7 {
8 deque<int> deqInt_A;
9 deqInt_A.assign(4, 888);
10
11 cout << "deqInt_A 中的元素为:" << endl;
12 for (deque<int>::iterator it = deqInt_A.begin(); it != deqInt_A.end(); it++)
13 {
14 cout << *it << " ";
15 }
16
17 return 0;
18 }
打印结果:
deque& operator=(const deque &deq);
//重载等号操作符
1 #include <iostream>
2 #include <deque>
3
4 using namespace std;
5
6 int main()
7 {
8 deque<int> deqInt_A, deqint_B;
9 deqInt_A.assign(4, 888);
10 deqint_B = deqInt_A; // operator= 重载,可以直接进行赋值
11
12 cout << "deqint_B 中的元素为:" << endl;
13 for (deque<int>::iterator it = deqint_B.begin(); it != deqint_B.end(); it++)
14 {
15 cout << *it << " ";
16 }
17
18 return 0;
19 }
打印结果:
deque.swap(deq);
// 将deque与本身的元素互换
1 #include <iostream>
2 #include <deque>
3
4 using namespace std;
5
6 int main()
7 {
8 deque<int> deqInt_A, deqint_B;
9 deqInt_A.assign(4, 111);
10 deqint_B.assign(5, 222);
11
12 cout << "deqInt_A 中的元素为:" << endl;
13 for (deque<int>::iterator it = deqInt_A.begin(); it != deqInt_A.end(); it++)
14 {
15 cout << *it << " ";
16 }
17 cout << "\ndeqint_B 中的元素为:" << endl;
18 for (deque<int>::iterator it = deqint_B.begin(); it != deqint_B.end(); it++)
19 {
20 cout << *it << " ";
21 }
22 cout << "\n\nswap 进行翻转" << endl;
23 deqInt_A.swap(deqint_B);
24 cout << "deqInt_A 中的元素为:" << endl;
25 for (deque<int>::iterator it = deqInt_A.begin(); it != deqInt_A.end(); it++)
26 {
27 cout << *it << " ";
28 }
29 cout << "\ndeqint_B 中的元素为:" << endl;
30 for (deque<int>::iterator it = deqint_B.begin(); it != deqint_B.end(); it++)
31 {
32 cout << *it << " ";
33 }
34 return 0;
35 }
打印结果:
=======================================================================================================================
STL——容器(deque) deque 的赋值 assign() operator=() swap()的更多相关文章
- cb14a_c++_顺序容器的操作7_赋值与交换(swap)_vector转list
cb14a_c++_顺序容器的操作7_赋值与交换(swap) vector数据赋值给list, slist.assign(svec.begin(), svec.end());//这样可以转 svec- ...
- STL——容器(deque) 构造 & 头尾添加删除元素
1.deque容器概念 deque是"double-ended queue"的缩写,和vector一样都是STL的容器,唯一不同的是:deque是双端数组,而vector是单端的. ...
- STL容器:deque双端队列学习
所谓deque,是"double-ended queue"的缩写; 它是一种动态数组形式,可以向两端发展,在尾部和头部插入元素非常迅速; 在中间插入元素比较费时,因为需要移动其它元 ...
- STL——容器(deque) deque 的大小
1. deque 的大小 deque.size(); //返回容器中元素的个数 1 #include <iostream> 2 #include <dequ ...
- STL——容器(deque) 元素的存取&迭代器
1. deque 的数据存取 这个部分和 vector 几乎一样 第一 使用下标操作 dequeName[0] = 100; //小心越界 第二 使用at 方法 如: dequeName.at(2 ...
- STL容器分析--deque
deque,故名思义,双向队列.可以在头尾进行插入删除. 而STL中采用了链表+线性表的数据结构来实现deque,因而除了满足双向队列的特点以外,还支持随机访问. 下面,贴一段代码. 总览:双向队列是 ...
- STL——容器(deque)deque 的删除 clear() erase()
deque.clear(); //移除容器的所有数据 1 #include <iostream> 2 #include <deque> 3 4 using namespace ...
- STL——容器(deque)deque 的插入 insert()
deque.insert(pos,elem); //在pos位置插入一个elem元素的拷贝,返回新数据的位置. 1 #include <iostream> 2 #include <d ...
- ACM常用STL容器
// STL(标准模板库),由三大部分组成:容器,算法,迭代器 // STL六大组件:container(容器),algorthm(算法),iterator(迭代器) // function obje ...
随机推荐
- Python面试题_初级版
1.如何在一个函数内部修改全局变量 a=5 def fn(): a=4 fn() print(a) # 5 #在一个函数内部修改全局变量 a=5 def fn(): global a a=4 fn() ...
- Java web 自动备份数据库和log4j日志
利用监听自动备份 web.xml <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns: ...
- Hadoop大数据平台搭建之前期配置(2)
环境:CentOS 7.4 (1708 DVD) 工具:VMware.MobaXterm 一. 克隆大数据集群 1. 选中已经进行了基本配置的虚拟机,进行克隆. 2. 此处改为"创建完整克 ...
- 微课制作软件Camtasia中如何添加并编辑字幕?
除了能录制视频以外,Camtasia还能直接把录制下来的视频进行剪辑,并添加视频字幕等等一些后期效果.今天我们就来看一看字幕的添加方法. 导入视频 微课制作软件Camtasia录制的视频,默认在软件& ...
- leetcode 56合并区间 java
//先排序,将左区间小的放在前面,然后如果前一个的右区间大于下一个的左区间,则可以合并,分别用两个下标指向当前的大区间和将要考察的小区间 class Solution { public int[ ...
- web服务器是啥
什么是web服务器 参考 https://www.cnblogs.com/zhaoyl/archive/2012/10/10/2718575.html 了解nginx之前,先了解下什么是web服务器吧 ...
- Leetcode 周赛#202 题解
本周的周赛题目质量不是很高,因此只给出最后两题题解(懒). 1552 两球之间的磁力 #二分答案 题目链接 题意 有n个空篮子,第i个篮子位置为position[i],现希望将m个球放到这些空篮子,使 ...
- 安装Linux软件时遇到这个问题,如何解决?
提示 Could not get lock /var/lib/dpkg/lock 报错? 有些小伙伴在使用 apt 包管理器更新或安装软件时,可能会遇到过诸如以下的错误提示: E: Could not ...
- CentOS下配置VNC
配置桌面 # 安装gnome桌面环境 yum groupinstall Desktop -y # 安装中文语言支持包(可选) yum groupinstall 'Chinese Support' -y ...
- 从零做网站开发:基于Flask和JQuery,实现表格管理平台
摘要:本文将为大家带来基于Flask框架和JQuery实现管理平台网站的开发功能. [写在前面] 你要开发网站? 嗯.. 会Flask吗? 什么东西,没听过... 会JQuery吗? 是python的 ...