码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2020-05-03 21:51:02      阅读:112      评论:0      收藏:0      [点我收藏+]

标签:out   name   col   pre   技术   insert   返回   return   alt   

deque.insert(pos,elem);

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

 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 
14     cout << "遍历deqInt_A" << endl;
15     for (deque<int>::iterator it = deqInt_A.begin(); it != deqInt_A.end(); it++)
16     {
17         cout << *it << " ";
18     }
19 
20     //在首位置插入1个数
21     deqInt_A.insert(deqInt_A.begin(), 0);
22 
23     cout << "\ninsert 插入后,遍历deqInt_A" << endl;
24     for (deque<int>::iterator it = deqInt_A.begin(); it!= deqInt_A.end(); it++)
25     {
26         cout << *it << " ";
27     }
28     
29     return 0;
30 }

打印结果:

技术图片

 

 

deque.insert(pos,n,elem);

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

 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 
14     cout << "遍历deqInt_A" << endl;
15     for (deque<int>::iterator it = deqInt_A.begin(); it != deqInt_A.end(); it++)
16     {
17         cout << *it << " ";
18     }
19 
20     //在首位置+1处 插入2个0
21     deqInt_A.insert(deqInt_A.begin() + 1, 2, 0);
22 
23     cout << "\ninsert 插入后,遍历deqInt_A" << endl;
24     for (deque<int>::iterator it = deqInt_A.begin(); it!= deqInt_A.end(); it++)
25     {
26         cout << *it << " ";
27     }
28     
29     return 0;
30 }

打印结果:

技术图片

 

 

deque.insert(pos,beg,end);

//在pos位置插入[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 
14     deqInt_B.push_back(10);
15     deqInt_B.push_back(20);
16     deqInt_B.push_back(30);
17 
18     cout << "遍历deqInt_A" << endl;
19     for (deque<int>::iterator it = deqInt_A.begin(); it != deqInt_A.end(); it++)
20     {
21         cout << *it << " ";
22     }
23     cout << "\n遍历deqInt_B" << endl;
24     for (deque<int>::iterator it = deqInt_B.begin(); it != deqInt_B.end(); it++)
25     {
26         cout << *it << " ";
27     }
28 
29     //在首位置+1处 插入deqInt_B
30     deqInt_A.insert(deqInt_A.begin() + 1, deqInt_B.begin(), deqInt_B.end());
31     cout << "\n在首位置+1处 插入deqInt_B" << endl;
32     for (deque<int>::iterator it = deqInt_A.begin(); it!= deqInt_A.end(); it++)
33     {
34         cout << *it << " ";
35     }
36     
37     return 0;
38 }

打印结果:

技术图片

 

 

 

 

 

 

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

 

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

标签:out   name   col   pre   技术   insert   返回   return   alt   

原文地址:https://www.cnblogs.com/CooCoChoco/p/12823480.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!