deque容器03之大小操作
阿新 • • 發佈:2021-02-10
技術標籤:stl學習之dequec++stl
大小操作
函式原型:
#include<iostream>
using namespace std;
#include<deque>
//deque的大小操作
void p(const deque<int>& d)
{
for (deque<int>::const_iterator it = d.begin(); it != d.end(); it++)
{
//*it = 100; 加了const關鍵字後,就無法對資料進行修改
cout << *it << " " ;
}
cout << endl;
//判斷容器是否為空
if (d.empty())
{
cout << "容器為空" << endl;
}
else {
cout << "容器不為空" << endl;
//返回容器中元素個數
cout << "容器中元素個數:" << d.size() << endl;
}
}
void realApply()
{
deque<int>d1;
d1.push_back (5);
d1.push_back(2);
d1.push_back(0);
p(d1);
//重新指定長度,大於原長,會用預設值填充新位置
d1.resize(10);
p(d1);
//重新指定長度,小於原長,就會將超出部分的元素刪除
d1.resize(3);
p(d1);
//重新指定長度,大於原長,就用elem來填充新位置
d1.resize(10, 1);
p(d1);
}
int main()
{
realApply();
system("pause");
return 0;
}