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()的更多相关文章

  1. cb14a_c++_顺序容器的操作7_赋值与交换(swap)_vector转list

    cb14a_c++_顺序容器的操作7_赋值与交换(swap) vector数据赋值给list, slist.assign(svec.begin(), svec.end());//这样可以转 svec- ...

  2. STL——容器(deque) 构造 & 头尾添加删除元素

    1.deque容器概念 deque是"double-ended queue"的缩写,和vector一样都是STL的容器,唯一不同的是:deque是双端数组,而vector是单端的. ...

  3. STL容器:deque双端队列学习

    所谓deque,是"double-ended queue"的缩写; 它是一种动态数组形式,可以向两端发展,在尾部和头部插入元素非常迅速; 在中间插入元素比较费时,因为需要移动其它元 ...

  4. STL——容器(deque) deque 的大小

    1. deque 的大小 deque.size();              //返回容器中元素的个数 1 #include <iostream> 2 #include <dequ ...

  5. STL——容器(deque) 元素的存取&迭代器

    1. deque 的数据存取 这个部分和 vector 几乎一样 第一  使用下标操作 dequeName[0] = 100; //小心越界 第二  使用at 方法 如: dequeName.at(2 ...

  6. STL容器分析--deque

    deque,故名思义,双向队列.可以在头尾进行插入删除. 而STL中采用了链表+线性表的数据结构来实现deque,因而除了满足双向队列的特点以外,还支持随机访问. 下面,贴一段代码. 总览:双向队列是 ...

  7. STL——容器(deque)deque 的删除 clear() erase()

    deque.clear(); //移除容器的所有数据 1 #include <iostream> 2 #include <deque> 3 4 using namespace ...

  8. STL——容器(deque)deque 的插入 insert()

    deque.insert(pos,elem); //在pos位置插入一个elem元素的拷贝,返回新数据的位置. 1 #include <iostream> 2 #include <d ...

  9. ACM常用STL容器

    // STL(标准模板库),由三大部分组成:容器,算法,迭代器 // STL六大组件:container(容器),algorthm(算法),iterator(迭代器) // function obje ...

随机推荐

  1. cephfs根据存储池显示df容量

    前言 如果用cephfs比较多,应该都知道,在cephfs的客户端进行mount以后,看到的容量显示的是集群的总的容量,也就是你的总的磁盘空间是多少这个地方显示的就是多少 这个一直都是这样显示的,我们 ...

  2. ceph的jewel新支持的rbd-nbd

    jewel版本新增加了一个驱动NBD,允许librbd实现一个内核级别的rbd NBD相比较于kernel rbd: rbd-ko是根据内核主线走的,升级kernel rbd需要升级到相应的内核,改动 ...

  3. HW弹药库之红队作战手册

    红方人员实战手册 声明 Author : By klion Date : 2020.2.15 寄语 : 愿 2020 后面的每一天都能一切安好 分享初衷 一来, 旨在为 "攻击" ...

  4. python-Requests模块的使用

    1. Requests简介 Requests模块是一个用于网络访问的模块,其实类似的模块有很多,比如urllib,urllib2,httplib,httplib2,他们基本都提供相似的功能,那为什么R ...

  5. 推荐系统实践 0x05 推荐数据集MovieLens及评测

    推荐数据集MovieLens及评测 数据集简介 MoiveLens是GroupLens Research收集并发布的关于电影评分的数据集,规模也比较大,为了让我们的实验快速有效的进行,我们选取了发布于 ...

  6. Java 枚举 enum 详解

    本文部分摘自 On Java 8 枚举类型 Java5 中添加了一个 enum 关键字,通过 enum 关键字,我们可以将一组拥有具名的值的有限集合创建为一种新的类型,这些具名的值可以作为常规的程序组 ...

  7. 前端(web)知识-html

    前端由三部分组成: HTML(标签)--CSS(美化,修饰)--JS(执行指令) HTML(超文本标记语言,Hypertext Markup Language):是一种用于创建网页的标记语言. 本质上 ...

  8. leetcode151. 翻转字符串里的单词

    给定一个字符串,逐个翻转字符串中的每个单词. 示例 1:输入: "the sky is blue"输出: "blue is sky the"示例 2:输入: & ...

  9. C语言讲义——字符串库函数

    字符串库函数<string.h> 求字符串长度(不含结束符'\0'****) strlen(str) 字符串赋值(可能造成数组越界) strcpy(str," 水浒传 " ...

  10. Java基础教程——运算符

    运算符 算术运算符 加 减 乘 除 取余 自加 自减 + - * / % ++ -- public class 算术运算符 { public static void main(String[] arg ...