目录
1.deque构造函数,容器中数据不修改的方法,const_iterator编辑
2.deque赋值,=,assign
3.deque大小的操作,empty,size,resize
4.deque容器插入和删除,push_back,push_front,pop_back,pop_front,insert,clear,erase
5.deque数据存取 at,[],front,back,遍历
6.deque排序,sort,vector也有sort
1.deque构造函数,容器中数据不修改的方法,const_iterator
#include<iostream>
using namespace std;
#include<deque>
//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());//将[d1.begin(),d1.end()]赋值给d2
printDeque(d2);
deque<int>d3(10, 100);//将10个100赋值给d3
printDeque(d3);
deque<int>d4(d3);//拷贝构造
printDeque(d4);
}
int main()
{
test01();
system("pause");//按任意键继续
return 0;
}
2.deque赋值,=,assign
#include<iostream>
using namespace std;
#include<deque>
//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;
d2 = d1;
printDeque(d2);
//assign赋值
deque<int>d3;
d3.assign(d1.begin() + 1, d1.end());//输出:1 2 3 4 5 6 7 8 9
printDeque(d3);
deque<int>d4;
d4.assign(10,100);
printDeque(d4);
}
int main()
{
test01();
system("pause");//按任意键继续
return 0;
}
3.deque大小的操作,empty,size,resize
#include<iostream>
using namespace std;
#include<deque>
//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())//容器为空返回真(1),否则返回假
{
cout << "d1为空" << endl;
}
else
{
cout << "d1不为空" << endl;
cout << "d1的大小为:" << d1.size() << endl;
//deque容器没有容量的概念
}
//重新指定大小
d1.resize(15);//默认以0来填充
printDeque(d1);//
deque<int>d2(3, 1);
d2.resize(10,2);//以2来填充
printDeque(d2);//
d1.resize(5);//超出的被删除掉
printDeque(d1);
}
int main()
{
test01();
system("pause");//按任意键继续
return 0;
}
4.deque容器插入和删除,push_back,push_front,pop_back,pop_front,insert,clear,erase
#include<iostream>
using namespace std;
#include<deque>
//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);//输出:200 100 10 20
//尾删
d1.pop_back();
printDeque(d1);//输出:200 100 10
//头删
d1.pop_front();
printDeque(d1);//输出: 100 10
}
//insert插入
void test02()
{
deque<int>d1;
d1.push_back(10);
d1.push_back(20);
d1.push_front(100);
d1.push_front(200);
printDeque(d1);//输出:200 100 10 20
//insert插入
d1.insert(d1.begin(), 1000);
printDeque(d1);//输出:1000 200 100 10 20
d1.insert(d1.begin()+1,2,10000);
printDeque(d1);//输出:1000 10000 10000 200 100 10 20
//按照区间插入
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);//输出:1 2 3 1000 10000 10000 200 100 10 20
}
//删除
void test03()
{
deque<int>d1;
d1.push_back(10);
d1.push_back(20);
d1.push_front(100);
d1.push_front(200);
printDeque(d1);//输出:200 100 10 20
deque<int>::iterator it = d1.begin();
d1.erase(it + 2);//等价于d1.erase(d1.begin() + 2);
printDeque(d1);//输出:200 100 20
//按照区间方式删除,//左闭右开
d1.erase(d1.begin(), d1.begin() + 1);
printDeque(d1);//输出:100 20
//清空
d1.clear();
}
int main()
{
test01();
cout << "********************************" << endl;
test02();
cout << "********************************" << endl;
test03();
system("pause");//按任意键继续
return 0;
}
5.deque数据存取 at,[],front,back,遍历
#include<iostream>
using namespace std;
#include<deque>
//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>d;
d.push_back(10);
d.push_back(20);
d.push_back(30);
d.push_front(100);
d.push_front(100);
d.push_front(200);
d.push_front(300);
//300 200 100 10 20 30
//通过[]方式访问元素
for (int i = 0; i < d.size(); i++)
{
cout << d[i] << " ";
}
cout << endl;
//通过at方式访问元素
for (int i = 0; i < d.size(); i++)
{
cout << d.at(i) << " ";
}
cout << endl;
cout << "第一个元素" << d.front() << endl;
cout << "最后一个元素" << d.back() << endl;
}
int main()
{
test01();
system("pause");//按任意键继续
return 0;
}
6.deque排序,sort,vector也有sort
#include<iostream>
using namespace std;
#include<deque>
#include<algorithm>//标准算法头文件
//deque 容器排序
void printDeque(deque<int>& d)
{
for (int i=0;i<d.size();i++)
{
cout<<d[i]<<" ";
}
cout << endl;
}
void test01()
{
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);
printDeque(d);//300 200 100 10 20 30
//排序,默认升序
//对于支持随机访问的迭代器,都可以利用sort算法直接对其进行排序
//vector容器也可以利用sort排序
sort(d.begin(),d.end());//需要头文件#include<algorithm>
cout << "排序后:" << endl;
printDeque(d);//10 20 30 100 200 300
}
int main()
{
test01();
system("pause");//按任意键继续
return 0;
}