3.3.1deque容器基本概念
- deque容器的迭代器也是支持随机访问的
3.3.2deque构造函数
代码
#include<iostream>
using namespace std;
#include<deque>
void printDeque(const deque<int>&d) //只读状态
{
for (deque<int>::const_iterator it = d.begin(); it != d.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
void test01()
{
deque<int>d1;
for (int i = 0; i < 10; i++)
{
d1.push_back(i);
}
printDeque(d1);
deque<int>d2(d1.begin(), d1.end());
printDeque(d2);
deque<int>d3(10, 100);
printDeque(d3);
deque<int>d4(d3);
printDeque(d4);
}
int main()
{
test01();
system("pause");
return 0;
}
总结:deque容器和vector容器的构造方式几乎一致,灵活使用即可。
3.3.3deque赋值操作
代码
#include<iostream>
using namespace std;
#include<deque>
void printDeque(const deque<int>&d)
{
for (deque<int>::const_iterator it = d.begin(); it != d.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
void test01()
{
deque<int>d1;
for (int i = 0; i < 10; i++)
{
d1.push_back(i);
}
printDeque(d1);
//operator =赋值
deque<int>d2;
d2 = d1;
printDeque(d2);
//assign赋值
deque<int>d3;
d3.assign(d1.begin(), d1.end());
printDeque(d3);
deque<int>d4;
d4.assign(10, 100);
printDeque(d4);
}
int main()
{
test01();
system("pause");
return 0;
}
总结:deque赋值操作与vector相同,需熟练掌握
3.3.4deque大小操作
代码
#include<iostream>
using namespace std;
#include<deque>
void printDeque(const deque<int>&d)
{
for (deque<int>::const_iterator it = d.begin(); it != d.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
void test01()
{
deque<int>d1;
for (int i = 0; i < 10; i++)
{
d1.push_back(i);
}
printDeque(d1);
//判断容器是否为空
if (d1.empty())
{
cout << "d1为空!" << endl;
}
else
{
cout << "d1不为空!" << endl;
cout << "d1的大小为:" << d1.size() << endl;
//deque没有容量
}
//改变容器大小
d1.resize(15,1);
printDeque(d1);
d1.resize(5);
printDeque(d1);
}
int main()
{
test01();
system("pause");
return 0;
}
总结:deque没有容量的概念
3.3.5deque插入与删除
代码
#include<iostream>
using namespace std;
#include<deque>
void printDeque(const deque<int>&d)
{
for (deque<int>::const_iterator it = d.begin(); it != d.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
void test01()
{
deque<int>d1;
//尾插
d1.push_back(10);
d1.push_back(20);
//头插
d1.push_front(100);
d1.push_front(200);
printDeque(d1);
//尾删
d1.pop_back();
printDeque(d1);
//头删
d1.pop_front();
printDeque(d1);
}
void test02()
{
deque<int>d1;
//尾插
d1.push_back(10);
d1.push_back(20);
//头插
d1.push_front(100);
d1.push_front(200);
printDeque(d1);
//insert插入
d1.insert(d1.begin(), 1000);
printDeque(d1);
d1.insert(d1.begin(),2, 10000);
printDeque(d1);
//按照区间进行插入
deque<int>d2;
d2.push_back(1);
d2.push_back(2);
d2.push_back(3);
d1.insert(d1.begin(), d2.begin(), d2.end());
printDeque(d1);
}
void test03()
{
deque<int>d1;
d1.push_back(1);
d1.push_back(2);
d1.push_back(3);
//删除——指定位置
deque<int>::iterator it = d1.begin();
it++;
d1.erase(it);
printDeque(d1);
//按区间方式删除
//d1.erase(d1.begin(), d1.end());
d1.clear();
printDeque(d1);
}
int main()
{
test03();
system("pause");
return 0;
}
总结:插入和删除提供的位置是迭代器
3.3.6deque数据存取
代码
#include<iostream>
using namespace std;
#include<deque>
void test01()
{
deque<int>d1;
d1.push_back(10);
d1.push_back(20);
d1.push_back(30);
d1.push_front(100);
d1.push_front(200);
d1.push_front(300);
for (int i = 0; i < d1.size(); i++)
{
cout << d1[i] << " ";
}
cout << endl;
for (int i = 0; i < d1.size(); i++)
{
cout << d1.at(i) << " ";
}
cout << endl;
cout << "第一个元素为:" << d1.front() << endl;
cout << "最后一个元素为:" << d1.back() << endl;
}
int main()
{
test01();
system("pause");
return 0;
}
总结:
- 除了用迭代器获取deque容器中元素,[]和at也可以
- front返回第一个元素,back返回最后一个元素
3.3.7deque排序
代码
#include<iostream>
using namespace std;
#include<deque>
#include<algorithm>
void printDeque(const deque<int>&d)
{
for (deque<int>::const_iterator it = d.begin(); it != d.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
void test01()
{
deque<int>d1;
d1.push_back(10);
d1.push_back(20);
d1.push_back(30);
d1.push_front(100);
d1.push_front(200);
d1.push_front(300);
printDeque(d1);
sort(d1.begin(), d1.end());
cout << "排序后:" << endl;
printDeque(d1);
}
int main()
{
test01();
system("pause");
return 0;
}
总结:sort算法非常实用,使用时包含头文件algorithm即可