0.前言
1.deque构造函数
#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++){//*it = 100; //加了const就不能再修改容器中的数据了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();//test02();//**************************************system("pause");return 0;
}
2.deque赋值操作
#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++){//*it = 100;//加了const就不能再修改容器中的数据了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(d2.begin(), d2.end());printDeque(d3);deque<int>d4;d4.assign(10, 200);printDeque(d4);
}int main() {test01();//test02();//**************************************system("pause");return 0;
}
3.deque大小操作
#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()){cout << "d1为空" << endl;}else{cout << "d1不为空" << endl;cout << "d1.size = " << d1.size() << endl;//deque容器没有容量的概念 d1.capacity() 没有这个接口 deque是无限扩容的}//重新指定大小d1.resize(15); //扩容的默认值为0printDeque(d1);d1.resize(20, 1);printDeque(d1);d1.resize(5);printDeque(d1);
}int main() {test01();//test02();//**************************************system("pause");return 0;
}
4.deque插入和删除
#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);//200 100 10 20printDeque(d1);//尾删d1.pop_back();//200 100 10printDeque(d1);//头删d1.pop_front();// 100 10printDeque(d1);
}//插入
void test02()
{deque<int>d1;d1.push_back(10);d1.push_back(20);d1.push_front(100);d1.push_front(200);// 200 100 10 20printDeque(d1);//insert插入d1.insert(d1.begin(), 1000);// 1000 200 100 10 20printDeque(d1);d1.insert(d1.begin(), 2, 10000);// 10000 1000 200 100 10 20printDeque(d1);deque<int>d2 = { 10,20,30 };d2.push_back(40);// d2 : 10 20 30 40d1.insert(d1.begin(), d2.begin(), d2.end()); //在指定位置插入一个区间printDeque(d1);
}//删除和清空
void test03()
{deque<int>d1;d1.push_back(10);d1.push_back(20);d1.push_front(100);d1.push_front(200);//删除deque<int>::iterator it = d1.begin();it++; //用这种方式可以指定删除哪个位置上数据d1.erase(it);//200 10 20printDeque(d1);//按区间方式删除//d1.erase(d1.begin(),d1.end()); //结合80行代码的方式便可以指定删除指定区间的数据//清空d1.clear();printDeque(d1);
}int main()
{cout << "test01: " << endl;test01();cout << endl << "test02: " << endl;test02();cout << endl << "test03: " << endl;test03();//**************************************system("pause");return 0;
}
5.deque数据存取
#include <iostream>
using namespace std;#include <deque>
//deque数据存取
void test01()
{deque<int>d1;d1.push_back(10);d1.push_back(20);d1.push_front(30);d1.push_front(40);//通过[]方式访问元素for (int i = 0; i < d1.size(); i++){cout << d1[i] << " ";}cout << endl;//通过.at()方式访问元素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();//test02();//test03();//**************************************system("pause");return 0;
}
6.deque排序
#include <iostream>
using namespace std;#include <deque>
#include <algorithm>
//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(1);d1.push_back(2);d1.push_front(3);d1.push_front(4);cout << "排序前:" << endl;printDeque(d1);//排序 默认排序规则 从小到大 升序//对于支持随机访问的迭代器的容器,//都可以利用sort算法直接对其进行排序。//vector容器也可以利用sort进行排序sort(d1.begin(), d1.end());cout << "排序后:" << endl;printDeque(d1);}
int main()
{test01();//test02();//test03();//**************************************system("pause");return 0;
}