deque.insert(pos,elem);

//在pos位置插入一个elem元素的拷贝,返回新数据的位置。

  1. 1 #include <iostream>
  2. 2 #include <deque>
  3. 3
  4. 4 using namespace std;
  5. 5
  6. 6 int main()
  7. 7 {
  8. 8 deque<int> deqInt_A, deqInt_B;
  9. 9
  10. 10 deqInt_A.push_back(1);
  11. 11 deqInt_A.push_back(2);
  12. 12 deqInt_A.push_back(3);
  13. 13
  14. 14 cout << "遍历deqInt_A" << endl;
  15. 15 for (deque<int>::iterator it = deqInt_A.begin(); it != deqInt_A.end(); it++)
  16. 16 {
  17. 17 cout << *it << " ";
  18. 18 }
  19. 19
  20. 20 //在首位置插入1个数
  21. 21 deqInt_A.insert(deqInt_A.begin(), 0);
  22. 22
  23. 23 cout << "\ninsert 插入后,遍历deqInt_A" << endl;
  24. 24 for (deque<int>::iterator it = deqInt_A.begin(); it!= deqInt_A.end(); it++)
  25. 25 {
  26. 26 cout << *it << " ";
  27. 27 }
  28. 28
  29. 29 return 0;
  30. 30 }

打印结果:

deque.insert(pos,n,elem);

//在pos位置插入n个elem数据,无返回值。

  1. 1 #include <iostream>
  2. 2 #include <deque>
  3. 3
  4. 4 using namespace std;
  5. 5
  6. 6 int main()
  7. 7 {
  8. 8 deque<int> deqInt_A, deqInt_B;
  9. 9
  10. 10 deqInt_A.push_back(1);
  11. 11 deqInt_A.push_back(2);
  12. 12 deqInt_A.push_back(3);
  13. 13
  14. 14 cout << "遍历deqInt_A" << endl;
  15. 15 for (deque<int>::iterator it = deqInt_A.begin(); it != deqInt_A.end(); it++)
  16. 16 {
  17. 17 cout << *it << " ";
  18. 18 }
  19. 19
  20. 20 //在首位置+1处 插入2个0
  21. 21 deqInt_A.insert(deqInt_A.begin() + 1, 2, 0);
  22. 22
  23. 23 cout << "\ninsert 插入后,遍历deqInt_A" << endl;
  24. 24 for (deque<int>::iterator it = deqInt_A.begin(); it!= deqInt_A.end(); it++)
  25. 25 {
  26. 26 cout << *it << " ";
  27. 27 }
  28. 28
  29. 29 return 0;
  30. 30 }

打印结果:

deque.insert(pos,beg,end);

//在pos位置插入[beg,end)区间的数据,无返回值

  1. 1 #include <iostream>
  2. 2 #include <deque>
  3. 3
  4. 4 using namespace std;
  5. 5
  6. 6 int main()
  7. 7 {
  8. 8 deque<int> deqInt_A, deqInt_B;
  9. 9
  10. 10 deqInt_A.push_back(1);
  11. 11 deqInt_A.push_back(2);
  12. 12 deqInt_A.push_back(3);
  13. 13
  14. 14 deqInt_B.push_back(10);
  15. 15 deqInt_B.push_back(20);
  16. 16 deqInt_B.push_back(30);
  17. 17
  18. 18 cout << "遍历deqInt_A" << endl;
  19. 19 for (deque<int>::iterator it = deqInt_A.begin(); it != deqInt_A.end(); it++)
  20. 20 {
  21. 21 cout << *it << " ";
  22. 22 }
  23. 23 cout << "\n遍历deqInt_B" << endl;
  24. 24 for (deque<int>::iterator it = deqInt_B.begin(); it != deqInt_B.end(); it++)
  25. 25 {
  26. 26 cout << *it << " ";
  27. 27 }
  28. 28
  29. 29 //在首位置+1处 插入deqInt_B
  30. 30 deqInt_A.insert(deqInt_A.begin() + 1, deqInt_B.begin(), deqInt_B.end());
  31. 31 cout << "\n在首位置+1处 插入deqInt_B" << endl;
  32. 32 for (deque<int>::iterator it = deqInt_A.begin(); it!= deqInt_A.end(); it++)
  33. 33 {
  34. 34 cout << *it << " ";
  35. 35 }
  36. 36
  37. 37 return 0;
  38. 38 }

打印结果:

===================================================================================================================

STL——容器(deque)deque 的插入 insert()的更多相关文章

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

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

  2. STL——容器(Set & multiset) insert 的返回值 和 pair 的用法

    1. 使用 insert 插入时的返回值: 将一个元素插入 (insert) 到 set 或 multiset 中时,如果插入失败返回的类型是一个 pair 的自定类型,insert 源码如下: in ...

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

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

  4. STL容器分析--deque

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

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

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

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

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

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

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

  8. STL——容器(deque) deque 的赋值 assign() operator=() swap()

    deque 的赋值分下边4种方法: deque.assign(beg,end); //将[beg, end)区间中的数据拷贝赋值给本身.注意该区间是左闭右开的区间. 1 #include <io ...

  9. STL容器存储的内容动态分配情况下的内存管理

    主要分两种情况:存储的内容是指针:存储的内容是实际对象. 看以下两段代码, typedef pair<VirObjTYPE, std::list<CheckID>*> VirO ...

随机推荐

  1. 如何个性化定制iview中的table样式

    使用renderHeader.render函数 例子: column:[ {}, ..., { title:'较年初占比变化', key:''lastYearChange, renderHeader: ...

  2. day02-业务服务监控

    提供大量第三方工具,可以开发企业级服务监控平台,本章涉及文件与目录差异对比.HTTP质量监控.邮件告警等内容一.文件内容差异比对1.示例1 d = difflib.Differ() diff = d. ...

  3. kali 系列学习02 - 被动扫描

    被动扫描是指目标无法察觉的情况下进行信息收集,注意有经验的渗透工程师会在信息收集上花费整个测试过程一半以上的时间,信息量太大,需要自动化的信息收集工具. 一.借鉴<kali linux2 网络渗 ...

  4. sqlilab less11-less18

    less-11 uname和passwd直接带入查询,万能密码 sqlmap自动搜索表单,或者抓包后用-r参数 less-12 post数据用小括号进行包裹,构造万能密码") or 1=1 ...

  5. (msf使用)msfconsole - meterpreter

    [msf] msfconsole meterpreter 对于这款强大渗透测试框架,详情介绍可看这里:metasploit 使用教程 对于msfconsole, Kali Linux 自带.只需用命令 ...

  6. 怎么在word里编辑插入数学公式?

    大学时代我们都有一个共同的噩梦--高数.每次上完高数课都有一些数学公式.可是我们最难的就是想用电脑在word中做笔记的时候该怎样用word插入公式.Word中自有的公式太少,新公式又太难输入.这也是一 ...

  7. keras实现MobileNet

    利用keras实现MobileNet,并以mnist数据集作为一个小例子进行识别.使用的环境是:tensorflow-gpu 2.0,python=3.7 , GTX-2070的GPU 1.导入数据 ...

  8. 精尽MyBatis源码分析 - MyBatis-Spring 源码分析

    该系列文档是本人在学习 Mybatis 的源码过程中总结下来的,可能对读者不太友好,请结合我的源码注释(Mybatis源码分析 GitHub 地址.Mybatis-Spring 源码分析 GitHub ...

  9. Mysql命令、常用函数

    一.sql命令行 查看数据库 show database : 选择使用的数据库 use 数据库名  : 查看表 show tables ; 查询表 select * from 表名     高版本my ...

  10. 六. Vue CLI详解

    1. Vue CLI理解 1.1 什么是Vue CLI 如果你只是简单写几个Vue的Demo程序, 那么你不需要Vue CLI,如果你在开发大型项目那么你需要它, 并且必然需要使用Vue CLI. 使 ...