deque容器

#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;
}

  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值