deque容器
前面学习了deque容器的构造方式、赋值操作和获取容器大小以及重新指定大小的操作,接下来学习,deque容器的插入和删除操作、数据存取以及排序操作。
1 deque 插入和删除
功能描述: 向deque容器中插入和删除数据
在前面的学习中,了解了deque容器是双端数组,可以在头部、尾部以及指定位置插入数据,那么接下来分头尾和指定位置两部分来学习插入和删除操作。
函数原型:
1. 两端插入操作:
push_back(elem);
————在容器尾部添加一个数据push_front(elem);
————在容器头部插入一个数据pop_back();
——————-删除容器最后一个数据pop_front();
——————-删除容器第一个数据
2. 指定位置操作:
-
insert(pos,elem);
————–在pos位置插入一个elem元素的拷贝,返回新数据的位置。 -
insert(pos,n,elem);
———–在pos位置插入n个elem数据,无返回值。 -
insert(pos,beg,end);
———-在pos位置插入[beg,end)区间的数据,无返回值。 -
clear();
—————————-清空容器的所有数据 -
erase(beg,end);
——————删除[beg,end)区间的数据,返回下一个数据的位置。 -
erase(pos);
————————删除pos位置的数据,返回下一个数据的位置。
代码示例:
void printDeque(const deque<int>& d)
{
for (deque<int>::const_iterator it = d.begin(); it != d.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
//1、两端插入
void test1()
{
int len = 35;
cout << "两端操作:" << endl;
deque<int> d1;
d1.push_back(10);
d1.push_back(20);
d1.push_back(30);
cout << "d1 尾插\t" << endl << "d1: ";
printDeque(d1);
cout << string(len, '-') << endl;
d1.push_front(100);
d1.push_front(200);
d1.push_front(300);
cout << "d1 头插\t" << endl << "d1: ";
printDeque(d1);
cout << string(len, '-') << endl;
d1.pop_back();
cout << "d1 尾删\t" << endl << "d1: ";
printDeque(d1);
cout << string(len, '-') << endl;
d1.pop_front();
cout << "d1 头删\t" << endl << "d1: ";
printDeque(d1);
cout << endl;
}
//2、指定位置
void test2()
{
cout << "指定位置操作:" << endl;
int len = 50;
deque<int> d1;
d1.push_back(10);
d1.push_back(20);
d1.push_front(100);
d1.push_front(200);
cout << "d1 插入前\t" << endl << "d1: ";
printDeque(d1);
cout << string(len, '-') << endl;
//1、insert插入-在pos位置 插入一个elem元素的拷贝
d1.insert(d1.begin(), 30);//返回新数据的位置
cout << "d1 insert 头部 插入单个数据\t" << endl << "d1: ";
printDeque(d1);
cout << string(len, '-') << endl;
//2、insert插入-在pos位置 插入n个elem元素的拷贝
d1.insert(d1.end(), 2, 15);
cout << "d1 insert 尾部 插入n个数据\t" << endl << "d1: ";
printDeque(d1);
cout << string(len, '-') << endl;
//3、insert插入-在pos位置 插入[beg,end)区间的数据
deque<int> d2;
d2.push_back(1);
d2.push_back(2);
d2.push_back(3);
cout << "d1 insert 头部 插入区间\t" << endl << "d1: ";
d1.insert(d1.begin(), d2.begin(), d2.end());//在d1的头部插入d2的[ )区间
printDeque(d1);
cout << string(len, '-') << endl;
}
//3、删除
void test3()
{
int len = 60;
deque<int> d1;
for (int i = 1; i < 11; i++)
{
d1.push_back(i * 10);
}
cout << "d1 删除前\t" << endl << "d1: ";
printDeque(d1);
cout << string(len, '-') << endl;
//1、指定位置删除
int select = 0;
cout << "请输入要删除的数据位置,1-10" << endl;
cin >> select;
deque<int>::iterator it = d1.begin();
while (--select)
{
it++;
}
d1.erase(it);
cout << "d1 指定位置删除后\t" << endl << "d1: ";
printDeque(d1);
cout << string(len, '-') << endl;
//2、区间删除
//d1.erase(d1.begin(), d1.end());//相当于清空
//3、清空
d1.clear();
cout << "d1 清空后\t" << endl << "d1: ";
printDeque(d1);
cout << string(len, '-') << endl;
}
注意: insert传入的都是迭代器
总结:
- 插入和删除提供的位置是迭代器!
- 尾插 — push_back
- 尾删 — pop_back
- 头插 — push_front
- 头删 — pop_front
2 deque 数据存取
功能描述: 对deque 中的数据的存取操作
函数原型:
at(int idx);
———返回索引idx所指的数据operator[];
———–返回索引idx所指的数据front();
—————返回容器中第一个数据元素back();
—————–返回容器中最后一个数据元素
代码示例:
void test1()
{
int len = 35;
deque<int> d;
d.push_back(10);
d.push_back(20);
d.push_back(30);
d.push_front(100);
d.push_front(200);
d.push_front(300);
cout << "[ ]访问\t" << endl << "d: ";
for (int i = 0; i < d.size(); ++i)
{
cout << d[i] << " ";
}
cout << endl << string(len, '-') << endl;
cout << "at访问\t" << endl << "d: ";
for (int i = 0; i < d.size(); ++i)
{
cout << d.at(i) << " ";
}
cout << endl << string(len, '-') << endl;
cout << "第一个元素:" << d.front() << endl;
cout << "最后一个元素:" << d.back() << endl;
cout << string(len, '-') << endl;
cout << endl;
}
总结:
- 访问方式和vector容器的一样
- 除了用迭代器获取deque容器中元素,[ ]和at也可以
- front返回容器第一个元素
- back返回容器最后一个元素
3 deque 排序
功能描述: 利用算法实现对deque容器进行排序
算法: sort(iterator beg, iterator end)
——对beg和end区间内元素进行排序
代码示例:
void printDeque(const deque<int>& d)
{
for (int i = 0; i < d.size(); i++)
{
cout << d[i] << " ";
}
cout << endl;
}
void test()
{
deque<int> d;
d.push_back(50);
d.push_back(30);
d.push_back(90);
d.push_back(15);
d.push_back(100);
d.push_back(88);
cout << "排序前 d:\t" << endl;
printDeque(d);
sort(d.begin(), d.end());
cout << "排序前 d:\t" << endl;
printDeque(d);
}
注意:
- sort 需要包含头文件
- sort 排序,默认是升序排序 从小到大
- sort 支持随机访问的迭代器的容器 deque vector
总结: sort算法非常实用,使用时包含头文件algorithm即可