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

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

时间:2020-05-03 01:02:32      阅读:68      评论:0      收藏:0      [点我收藏+]

标签:长度   ===   返回   code   name   int   img   begin   iterator   

1. deque 的大小

  • deque.size();              //返回容器中元素的个数
 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, 111);
10 
11     cout << "deqInt_A 中的元素个数为:"<< deqInt_A.size() << endl;
12 
13     return 0;
14 }

打印结果:

技术图片

 

 

  • deque.empty();       //判断容器是否为空
 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 
11     if (deqInt_A.empty())
12     {
13         cout << "deqInt_A 为空" << endl;
14     }
15     else
16     {
17         cout << "deqInt_A 不为空" << endl;
18     }
19     if (deqInt_B.empty())
20     {
21         cout << "deqInt_B 为空" << endl;
22     }
23     else
24     {
25         cout << "deqInt_B 不为空" << endl;
26     }
27     
28     return 0;
29 }

打印结果

技术图片

 

 

  • deque.resize(num);       //重新指定容器的长度为num,若容器变长,则以默认值0填充新位置。如果容器变短,则末尾超出容器长度的元素被删除。

 

 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(5, 111);
10 
11     for (deque<int>::iterator it = deqInt_A.begin(); it!= deqInt_A.end(); it++)
12     {
13         cout << *it << " ";
14     }
15     
16     cout << "\n使用 resize 扩容" << endl;
17     deqInt_A.resize(10);
18     for (deque<int>::iterator it = deqInt_A.begin(); it != deqInt_A.end(); it++)
19     {
20         cout << *it << " ";
21     }
22 
23     return 0;
24 }

 

打印结果:

技术图片

 

 

  • deque.resize(num, elem);  //重新指定容器的长度为num,若容器变长,则以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(5, 111);
10 
11     for (deque<int>::iterator it = deqInt_A.begin(); it!= deqInt_A.end(); it++)
12     {
13         cout << *it << " ";
14     }
15     
16     cout << "\n使用 resize 扩容" << endl;
17     deqInt_A.resize(10, 222);
18     for (deque<int>::iterator it = deqInt_A.begin(); it != deqInt_A.end(); it++)
19     {
20         cout << *it << " ";
21     }
22 
23     return 0;
24 }

 

打印结果:

技术图片

 

 

 

 

 

 

 

 

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

 

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

标签:长度   ===   返回   code   name   int   img   begin   iterator   

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

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