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用户认证格式写错的问题

    问题三: CephFS(James Gallagher) 问题原文 Hi, I'm looking to implement the CephFS on my Firefly release (v0. ...

  2. python文件操作与编解码

    1 # 文件操作 2 3 ''' 4 1.文件路径:要知道文件的路径 5 6 2.编码方式:要知道文件是什么编码的.utf-8 gbk...... 7 8 3.操作方式:要以什么样的方式进行打开这个文 ...

  3. API的使用(3)Arrays 类,Math类,三大特性--继承

    Arrays类 概述   java.util.Arrays此时主要是用来操作数组,里面提供了很多的操作API的方法.如[排序]和[搜索]功能.其所有的方法均为静态方法,调用起来非常简单. 操作数组的方 ...

  4. 深度分析:Java并发编程之线程池技术,看完面试这个再也不慌了!

    线程池的好处 Java中的线程池是运用场景最多的并发框架,几乎所有需要异步或并发执行任务的程序都可以使用线程池.在开发过程中,合理地使用线程池,相对于单线程串行处理(Serial Processing ...

  5. FL Studio音乐编曲入门教程

    有很多小伙伴给我们留言说使用FL Studio20一段时间后,虽然对这款音乐编曲软件的功能有了一个了解,但对它整个的编曲过程还不是太熟悉.所以今天我就给大家带来了FL Studio20这款音乐编曲软件 ...

  6. kube-flannel.yml 文件

    ---kind: ClusterRoleapiVersion: rbac.authorization.k8s.io/v1beta1metadata: name: flannelrules: - api ...

  7. Pypi项目包发布

    前言 用过python的人肯定对pip安装不陌生,pip安装的包的来源都是在Pypi上.为了能随时能使用自己的包,所以尝试一下将自己的包发布到Pypi上. 步骤 一.准备项目 一.创建目录结构 二.准 ...

  8. C语言讲义——内联函数

    如果一些函数被频繁调用,不断地有函数入栈(Stack),会造成栈空间的大量消耗. 对应这种问题,可以使用内联函数(inline). 编译器会将内联函数的代码整段插入到调用的位置. #include & ...

  9. miniconda安装及使用

    conda环境配置 安装conda [清华源下载地址](https://mirrors.tuna.tsinghua.edu.cn/anaconda/miniconda/) 官网或百度云网盘下载对应版本 ...

  10. 深入浅出之mysql索引--上

    当着小萌新之际,最近工作中遇到了mysql优化的相关问题,然后既然提到了优化,很多像我这样的小萌新不容置喙,肯定张口就是 建立索引 之类的. 那么说到底,索引到底是什么,它是怎么工作的?接下来就让我和 ...