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. fio的配置使用

    将fio-2.1.10.tar.gz拷贝到linux服务器的/usr/src/下 解压源码包: root@grandocean:/usr/src# tar xvf fio-2.1.10.tar.gz ...

  2. mysql_用命令行备份数据库

    MySQL数据库使用命令行备份|MySQL数据库备份命令 例如: 数据库地址:127.0.0.1 数据库用户名:root 数据库密码:pass 数据库名称:myweb 备份数据库到D盘跟目录 mysq ...

  3. TypeScript 引入第三方包,报无法找到模块错误

    以 react-router-dom 模块为例 1. npm加上 @types/ 根据报错提示尝试安装该库的TypeScript版本 (该库的 ts 声明文件),也就是在该库的名称前加上 @types ...

  4. [LeetCode题解]109. 有序链表转换二叉搜索树 | 快慢指针 + 递归

    题目描述 给定一个单链表,其中的元素按升序排序,将其转换为高度平衡的二叉搜索树. 本题中,一个高度平衡二叉树是指一个二叉树每个节点 的左右两个子树的高度差的绝对值不超过 1. 示例: 给定的有序链表: ...

  5. [LeetCode题解]24. 两两交换链表中的节点 | 递归

    方法一:递归 解题思路 递归法,假设后续链表已经完成交换,此时只需要对前两个节点进行交换,然后再连接上后续已交换的链表即可. 代码 /** * Definition for singly-linked ...

  6. 批量反编译.class

    使用dj java Decompiler软件,安装后,安装目录会有个jad.exe程序 控制台执行: jad -o -r -dF:\output_dir -sjava F:\class_root_di ...

  7. Cypress系列(90)- Cypress.Cookies 命令详解以及如何跨测试用例共享 Cookies

    如果想从头学起Cypress,可以看下面的系列文章哦 https://www.cnblogs.com/poloyy/category/1768839.html Cypress.Cookies 共有三个 ...

  8. Boom 3D快捷方式,让3D音效应用更便捷

    快捷方式是一种快速启动程序.打开程序功能的方法,巧妙地利用快捷键,可以大大加快我们使用Boom 3D的速度,可以让我们更好地享受3D音效. 接下来,就让小编演示一下怎么在不打开Boom 3D的情况下使 ...

  9. FL studio系列教程(十四):如何在FL Studio播放列表中排列样式

    我们在FL Studio中做好了节奏样式后就可以在播放列表窗口中进行乐曲的编排了.刚接触这款软件的同学肯定会对如何编排比较陌生但也比较憧憬的,因为它是从一个窗口到另一个窗口中的操作.其实明白了这里的知 ...

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

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