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. Freebsd10.2安装包升级pkg引起环境破坏的解决

    前言 freebsd10.2环境在安装一个新软件包的时候提示升级pkg到1.10.1,然后点击了升级,然后整个pkg环境就无法使用了 记录 升级完了软件包以后第一个错误提示 FreeBSD: /usr ...

  2. 限流10万QPS、跨域、过滤器、令牌桶算法-网关Gateway内容都在这儿

    一.微服务网关Spring Cloud Gateway 1.1 导引 文中内容包含:微服务网关限流10万QPS.跨域.过滤器.令牌桶算法. 在构建微服务系统中,必不可少的技术就是网关了,从早期的Zuu ...

  3. CSS浮动和清除浮动

    1.浮动float div配合float浮动来做页面的布局,浮动最常用的地方就是用来做布局.只有一个标签设置了浮动float属性,就会变成块级标签. <!DOCTYPE html> < ...

  4. Ubuntu linux系统下 su:出现: authentication failure的解决办法

    当出现这个问题后,尝试一下方法: $ sudo passwd rootEnter new UNIX password://此时输入你的密码Retype new UNIX password://再次输入 ...

  5. 早期javac编译器优化

    学习<深入了解Java虚拟机>有一段时间了,大概理解了Java从源代码编译到执行出结果的过程,也能明确的知道Java是半解释性语言.在执行源代码时,先通过Javac编译器对源代码进行词法分 ...

  6. MQ消息中间件,面试能问些什么?

    MQ消息中间件,面试能问些什么? 为什么使用消息队列?消息队列的优点和缺点? kafka.activemq.rabbitmq.rocketmq都有什么优缺点? 面试官角度分析: (1)你知不知道你们系 ...

  7. docker下启动单机nacos

    docker run --env MODE=standalone --name nacos -d -p 8848:8848 nacos/nacos-server 参数说明: MODE standalo ...

  8. 重新认识C++的"cin >>"、"cout <<" 简简单单 - 快快乐乐

    重新认识C++的"cin >>"."cout <<" 简简单单 - 快快乐乐 JERRY_Z. ~ 2020 / 11 / 24 转载请 ...

  9. CorelDRAW文件损坏的几种解决方法

    以前做好的CorelDRAW文件突然打不开了,或者是死机.非法操作等原因造成CorelDRAW文件损坏,有时打开源文件发现一片空白,源文件保存损坏无法打开怎么办?此时不要着急,你可以试试以下几种办法帮 ...

  10. Guitar Pro吉他指弹入门——美式指弹

    说起指弹吉他,很多身边的琴友首先反应到的是押尾桑,岸部真明,伍伍慧等等指弹艺术家的日式指弹.笔者在初涉指弹的时候,也是如此,但是随着学习的加深,首先认识到了汤米大神(Tommy Emmanuel),然 ...