您的位置:首页 > 产品设计 > UI/UE

STL学习系列三:Deque容器

2015-11-17 11:37 525 查看

1.Deque简介

deque是“double-ended queue”的缩写,和vector一样都是STL的容器,deque是双端数组,而vector是单端的。

deque在接口上和vector非常相似,在许多操作的地方可以直接替换。

deque可以随机存取元素(支持索引值直接存取, 用[]操作符或at()方法,这个等下会详讲)。

deque头部和尾部添加或移除元素都非常快速。但是在中部安插元素或移除元素比较费时。

2.deque对象的默认构造

deque采用模板类实现,deque对象的默认构造形式:deque<T> deq;

deque <int> deqInt; //一个存放int的deque容器。

deque <float> deq Float; //一个存放float的deque容器。

3.deque对象的带参数构造

deque(beg,end); //构造函数将[beg, end)区间中的元素拷贝给本身。注意该区间是左闭右开的区间。

deque(n,elem); //构造函数将n个elem拷贝给本身。

deque(const deque &deq); //拷贝构造函数。

#include<iostream>
using namespace std;
#include <deque>
void objPlay3()
{
deque<int> deqIntA;
deqIntA.push_back(1);
deqIntA.push_back(3);
deqIntA.push_back(5);
deqIntA.push_back(7);
deqIntA.push_back(9);

deque<int> deqIntB(deqIntA.begin(), deqIntA.end());      //1 3 5 7 9
deque<int> deqIntC(5, 8);                               //8 8 8 8 8
deque<int> deqIntD(deqIntA);                           //1 3 5 7 9
}
int main()
{
objPlay3();
return 0;
}


4.deque的赋值

deque.assign(beg,end); //将[beg, end)区间中的数据拷贝赋值给本身。注意该区间是左闭右开的区间。

deque.assign(n,elem); //将n个elem拷贝赋值给本身。

deque& operator=(const deque &deq); //重载等号操作符

deque.swap(deq); // 将vec与本身的元素互换

void objPlay4()
{
deque<int> deqIntA, deqIntB, deqIntC, deqIntD;
deqIntA.push_back(1);
deqIntA.push_back(3);
deqIntA.push_back(5);
deqIntA.push_back(7);
deqIntA.push_back(9);

deqIntB.assign(deqIntA.begin(), deqIntA.end());   // 1 3 5 7 9
deqIntC.assign(5, 8);                            //8 8 8 8 8
deqIntD = deqIntA;                              //1 3 5 7 9
deqIntC.swap(deqIntD);                         //C 和 D互换
}


5.deque的大小

deque.size(); //返回容器中元素的个数。

deque.empty(); //判断容器是否为空。

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

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

void objPlay5()
{
  deque<int> deqIntA;
  deqIntA.push_back(1);
  deqIntA.push_back(3);
  deqIntA.push_back(5);

  int iSize = deqIntA.size(); //3

  if (!deqIntA.empty())
  {
    deqIntA.resize(5); //1 3 5 0 0
    deqIntA.resize(7, 1); //1 3 5 0 0 1 1
    deqIntA.resize(2); //1 3
  }

}

6.deque末尾的添加移除操作

deque.push_back(elem); //在容器尾部添加一个数据

deque.push_front(elem); //在容器头部插入一个数据

deque.pop_back(); //删除容器最后一个数据

deque.pop_front(); //删除容器第一个数据

void objPlay6()
{
deque<int> deqInt;
deqInt.push_back(1);
deqInt.push_back(3);
deqInt.push_back(5);
deqInt.push_back(7);
deqInt.push_back(9);//此时1,3,5,7,9

deqInt.pop_front(); //弹出头部第一个元素
deqInt.pop_front();
deqInt.push_front(11);//头部添加元素
deqInt.push_front(13);
deqInt.pop_back();   //弹出最后一个 元素
deqInt.pop_back();
}


7.deque的数据存取

deque.at(idx); //返回索引idx所指的数据,如果idx越界,抛出out_of_range。

deque[idx]; //返回索引idx所指的数据,如果idx越界,不抛出异常,直接出错。

deque.front(); //返回第一个数据。

deque.back(); //返回最后一个数据

void objPlay7()
{
deque<int> deqInt;
deqInt.push_back(1);
deqInt.push_back(3);
deqInt.push_back(5);
deqInt.push_back(7);
deqInt.push_back(9);

int iA = deqInt.at(0);        //1
int iB = deqInt[1];            //3
deqInt.at(0) = 99;            //99
deqInt[1] = 88;                //88

int iFront = deqInt.front();//99
int iBack = deqInt.back();    //9
deqInt.front() = 77;        //77
deqInt.back() = 66;            //66
}


8.deque的插入

deque.insert(pos,elem); //在pos位置插入一个elem元素的拷贝,返回新数据的位置。

deque.insert(pos,n,elem); //在pos位置插入n个elem数据,无返回值。

deque.insert(pos,beg,end); //在pos位置插入[beg,end)区间的数据,无返回值。

void objPlay8()
{
deque<int> deqA;
deque<int> deqB;

deqA.push_back(1);
deqA.push_back(3);
deqA.push_back(5);
deqA.push_back(7);
deqA.push_back(9);

deqB.push_back(2);
deqB.push_back(4);
deqB.push_back(6);
deqB.push_back(8);

deqA.insert(deqA.begin(), 11);        //{11, 1, 3, 5, 7, 9}
deqA.insert(deqA.begin() + 1, 2, 33);        //{11,33,33,1,3,5,7,9}
deqA.insert(deqA.begin(), deqB.begin(), deqB.end());    //{2,4,6,8,11,33,33,1,3,5,7,9}

}


9.deque的删除

deque.clear(); //移除容器的所有数据

deque.erase(beg,end); //删除[beg,end)区间的数据,返回下一个数据的位置。

deque.erase(pos); //删除pos位置的数据,返回下一个数据的位置。

void objPlay9()
{

int intarr[10] = { 1, 3, 2, 3, 3, 3, 4, 3, 5, 3 };
//假设 deqInt 包含1, 3, 2, 3, 3, 3, 4, 3, 5, 3,删除容器中等于3的元素
deque<int> deqInt(intarr, intarr + 10);
for (deque<int>::iterator it = deqInt.begin(); it != deqInt.end();)    //小括号里不需写  ++it
{
if (*it == 3)
{
it = deqInt.erase(it);       //以迭代器为参数,删除元素3,并把数据删除后的下一个元素位置返回给迭代器。
//此时,不执行  ++it;
}
else
{
++it;
}
}

//删除deqInt的所有元素
deqInt.clear();            //容器为空

}


整理所有代码如下:

#include<iostream>
using namespace std;
#include <deque>
void objPlay3()
{
deque<int> deqIntA;
deqIntA.push_back(1);
deqIntA.push_back(3);
deqIntA.push_back(5);
deqIntA.push_back(7);
deqIntA.push_back(9);

deque<int> deqIntB(deqIntA.begin(), deqIntA.end()); //1 3 5 7 9
deque<int> deqIntC(5, 8); //8 8 8 8 8
deque<int> deqIntD(deqIntA); //1 3 5 7 9
}
void objPlay4()
{
deque<int> deqIntA, deqIntB, deqIntC, deqIntD;
deqIntA.push_back(1);
deqIntA.push_back(3);
deqIntA.push_back(5);
deqIntA.push_back(7);
deqIntA.push_back(9);

deqIntB.assign(deqIntA.begin(), deqIntA.end()); // 1 3 5 7 9
deqIntC.assign(5, 8); //8 8 8 8 8
deqIntD = deqIntA; //1 3 5 7 9
deqIntC.swap(deqIntD); //C 和 D互换
}
void objPlay5()
{
deque<int> deqIntA;
deqIntA.push_back(1);
deqIntA.push_back(3);
deqIntA.push_back(5);

int iSize = deqIntA.size(); //3

if (!deqIntA.empty())
{
deqIntA.resize(5); //1 3 5 0 0
deqIntA.resize(7, 1); //1 3 5 0 0 1 1
deqIntA.resize(2); //1 3
}

}
void objPlay6() { deque<int> deqInt; deqInt.push_back(1); deqInt.push_back(3); deqInt.push_back(5); deqInt.push_back(7); deqInt.push_back(9);//此时1,3,5,7,9 deqInt.pop_front(); //弹出头部第一个元素 deqInt.pop_front(); deqInt.push_front(11);//头部添加元素 deqInt.push_front(13); deqInt.pop_back(); //弹出最后一个 元素 deqInt.pop_back(); }
void objPlay7() { deque<int> deqInt; deqInt.push_back(1); deqInt.push_back(3); deqInt.push_back(5); deqInt.push_back(7); deqInt.push_back(9); int iA = deqInt.at(0); //1 int iB = deqInt[1]; //3 deqInt.at(0) = 99; //99 deqInt[1] = 88; //88 int iFront = deqInt.front();//99 int iBack = deqInt.back(); //9 deqInt.front() = 77; //77 deqInt.back() = 66; //66 }
void objPlay8() { deque<int> deqA; deque<int> deqB; deqA.push_back(1); deqA.push_back(3); deqA.push_back(5); deqA.push_back(7); deqA.push_back(9); deqB.push_back(2); deqB.push_back(4); deqB.push_back(6); deqB.push_back(8); deqA.insert(deqA.begin(), 11); //{11, 1, 3, 5, 7, 9} deqA.insert(deqA.begin() + 1, 2, 33); //{11,33,33,1,3,5,7,9} deqA.insert(deqA.begin(), deqB.begin(), deqB.end()); //{2,4,6,8,11,33,33,1,3,5,7,9} }
void objPlay9() { int intarr[10] = { 1, 3, 2, 3, 3, 3, 4, 3, 5, 3 }; //假设 deqInt 包含1, 3, 2, 3, 3, 3, 4, 3, 5, 3,删除容器中等于3的元素 deque<int> deqInt(intarr, intarr + 10); for (deque<int>::iterator it = deqInt.begin(); it != deqInt.end();) //小括号里不需写 ++it { if (*it == 3) { it = deqInt.erase(it); //以迭代器为参数,删除元素3,并把数据删除后的下一个元素位置返回给迭代器。 //此时,不执行 ++it; } else { ++it; } } //删除deqInt的所有元素 deqInt.clear(); //容器为空 }
int main()
{
objPlay3();
objPlay4();
objPlay5();
objPlay6();
objPlay7();
objPlay8();
objPlay9();
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: