#include <deque>
#include <iostream>
using namespace std;
class student {
public:
student(int _age) {
cout << "construct student, age:" << _age << endl;
this->age = _age;
}
student(const student& other) {
cout << "拷贝构造函数:" << endl;
this->age = other.age;
}
~student() {
cout << "descontruct student." << endl;
}
int getAge() {
return age;
}
private:
int age;
};
void demo1(void) {
//1.deque 类模板默认构造
deque<int> deqIntA;//存放int的deque容器
deque<float> deqFloat;//存放float的deque容器
deque<student> deqStu;//存放student的deque容器
deque<student*> pDeqStu;
deqIntA.push_back(1);
deqFloat.push_back(0.1f);
deqStu.push_back(student(18));
//2.deque带参构造
//给定区间
deque<int> deqIntB(deqIntA.begin(), deqIntA.end());
//指定n个元素进行构造
deque<int> deqIntC(10, 0);
//拷贝构造
deque<int> deqIntD(deqIntC);
cout << "deqIntD中的元素:" << endl;
for (unsigned int i = 0; i < deqIntD.size(); i++) {
cout << deqIntD[i] << " ";
}
cout << endl;
//deque头部和尾部的添加移除操作
//deqIntA.push_back(1);
deqIntA.push_back(2);
deqIntA.push_back(3);
deqIntA.push_back(4);
deqIntA.push_back(5);
deqIntA.push_back(6);
deqIntA.pop_front();//2 3 4 5 6
deqIntA.pop_front();//3 4 5 6
deqIntA.push_front(7);//7 3 4 5 6
deqIntA.push_front(8);//8 7 3 4 5 6
deqIntA.pop_back();//8 7 3 4 5
cout << "deqIntA中的元素:" << endl;
for (unsigned int i = 0; i < deqIntA.size(); i++) {
cout << deqIntA[i] << " ";
}
cout << endl;
system("pause");
return;
}
//迭代器存取和访问
void demo2(void) {
deque<int> deqIntA;
deqIntA.push_back(1);
deqIntA.push_back(2);
deqIntA.push_back(3);
deqIntA.push_back(4);
deqIntA.push_back(5);
//deqIntA.push_back(6);
int i1 = deqIntA.at(0); //i1=1
int i2 = deqIntA[1]; //i2=2
deqIntA.at(0) = 123;
deqIntA[1] = 456;
int iFront = deqIntA.front();
int iBack = deqIntA.back();
iFront++;
iBack++;
deqIntA.front() = iFront;
deqIntA.back() = iBack;
cout << "deqIntA中的元素:" << endl;
/*for (unsigned int i = 0; i < deqIntA.size(); i++) {
cout << deqIntA[i] << " ";
}*/
//普通迭代器
/*for (deque<int>::iterator it = deqIntA.begin(); it != deqIntA.end(); it++) {
cout << *it << " ";
}*/
//常量迭代器
/*deque<int>::const_iterator cit = deqIntA.cbegin();
for (; cit != deqIntA.end(); cit++) {
cout << *cit << " ";
}*/
//逆转的迭代器
for (deque<int>::reverse_iterator rit = deqIntA.rbegin(); rit != deqIntA.rend(); rit++) {
cout << *rit << " ";
}
cout << endl;
system("pause");
return;
}
//deque赋值与大小
void demo3(void) {
deque<int> deqIntA, deqIntB, deqIntC, deqIntD;
deqIntA.push_back(1);
deqIntA.push_back(2);
deqIntA.push_back(3);
deqIntA.push_back(4);
deqIntA.push_back(5);
int size = deqIntA.size();
deqIntA.resize(7);
//deqIntA.resize(3);
deqIntA.resize(8, 1);
for (deque<int>::iterator it = deqIntA.begin(); it != deqIntA.end(); it++) {
cout << *it << " ";
}
system("pause");
deqIntB.assign(deqIntA.begin(), deqIntA.end() - 1);//1 2 3 4
deqIntC.assign(4, 888);
deqIntD = deqIntA;
deqIntC.swap(deqIntD);
for (deque<int>::iterator it = deqIntD.begin(); it != deqIntD.end(); it++) {
cout << *it << " ";
}
}
//deque插入和删除
void demo4(void) {
deque<int> deqIntA, deqIntB;
deqIntA.push_back(1);
deqIntA.push_back(2);
deqIntA.push_back(3);
deqIntA.push_back(4);
deqIntB.push_back(11);
deqIntB.push_back(12);
deqIntB.push_back(13);
deqIntB.push_back(14);
//1.使用擦除的接口
//deqIntA.erase(deqIntA.begin() + 1);// 1 3 4
//deqIntA.erase(deqIntA.begin(), deqIntA.end() - 1);//4
//deqIntA.clear();
/*for (deque<int>::iterator it = deqIntA.begin(); it != deqIntA.end(); it++) {
cout << *it << " ";
}*/
//2.使用迭代器遍历删除
for (deque<int>::iterator it = deqIntA.begin(); it != deqIntA.end(); ) {
if (*it == 2) {
it = deqIntA.erase(it);
continue;
}
else {
cout << *it << " ";
it++;
}
}
system("pause");
deqIntA.insert(deqIntA.begin(), 0);//0 1 2 3 4
deqIntA.insert(deqIntA.begin() + 1, 2, 9);//0 9 9 1 2 3 4
deqIntA.insert(deqIntA.begin(), deqIntB.begin(), deqIntB.end());
for (deque<int>::iterator it = deqIntA.begin(); it != deqIntA.end(); it++) {
cout << *it << " ";
}
}
int main(void) {
demo4();
system("pause");
return 0;
}
12-18
1万+
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
08-20
220
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
01-11
1万+
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
09-11
“相关推荐”对你有帮助么?
-
非常没帮助
-
没帮助
-
一般
-
有帮助
-
非常有帮助
提交