deque容器
功能:双端数组,可以对头端进行插入操作。
1、构造函数
和vector容器的构造函数一样。
#include <deque> void print(const deque<int>& d) { for (deque<int>::const_iterator it = d.begin(); it != d.end(); it++) { //*it = 100; 容器中的数据不可修改 cout << *it << " "; } cout << endl; } void test1() { deque<int> d1; for (int i = 0; i < 10; i++) { d1.push_back(i); } print(d1); deque<int> d2(d1.begin(), d1.end()); print(d2); deque<int> d3(10, 100); print(d3); deque<int> d4(d3); print(d4); } int main() { test1(); system("pause"); return 0; }
2、赋值操作
和vector容器的赋值操作一样。
#include <deque> void print(const deque<int>& d) { for (deque<int>::const_iterator it = d.begin(); it != d.end(); it++) { cout << *it << " "; } cout << endl; } void test1() { deque<int> d1; for (int i = 0; i < 10; i++) { d1.push_back(i); } print(d1); //=赋值 deque<int> d2; d2 = d1; print(d2); //assign赋值 deque<int> d3; d3.assign(d1.begin(), d1.end()); print(d3); //n个elme deque<int> d4; d4.assign(10, 100); print(d4); } int main() { test1(); system("pause"); return 0; }
3、大小操作
deque没有容量,只有元素的个数。
#include <deque> void print(const deque<int>& d) { for (deque<int>::const_iterator it = d.begin(); it != d.end(); it++) { cout << *it << " "; } cout << endl; } void test1() { deque<int> d1; for (int i = 0; i < 10; i++) { d1.push_back(i); } print(d1); if (d1.empty()) { cout << "d1为空" << endl; } else { cout << "d1不为空" << endl; cout << "d1的大小" << d1.size() << endl; } //重新指定大小 //d1.resize(15); d1.resize(15, 1); print(d1); d1.resize(5); print(d1); } int main() { test1(); system("pause"); return 0; }
4、插入和删除
#include <deque> void print(const deque<int>& d) { for (deque<int>::const_iterator it = d.begin(); it != d.end(); it++) { cout << *it << " "; } cout << endl; } //两端操作 void test1() { deque<int> d1; //尾插 d1.push_back(10); d1.push_back(20); //头插 d1.push_front(100); d1.push_front(200); print(d1); //尾删 d1.pop_back(); print(d1);d1.push_front(200); print(d1); //尾删 d1.pop_back(); print(d1); //头删 d1.pop_front(); print(d1); } void test2() { deque<int> d2; d2.push_back(10); d2.push_back(20); d2.push_front(100); d2.push_front(200); print(d2); //insert插入 d2.insert(d2.begin(), 1000); //1000 200 100 10 20 print(d2); d2.insert(d2.begin(), 2, 10000); //10000 10000 1000 200 100 10 20 print(d2); //按照区间进行插入 deque<int> d3; d3.push_back(1); d3.push_back(2); d3.push_back(3); d2.insert(d2.begin(), d3.begin(), d3.end()); //1 2 3 10000 10000 1000 200 100 10 20 print(d2); } void test3() { deque<int> d2; d2.push_back(10); d2.push_back(20); d2.push_front(100); d2.push_front(200); //删除 deque<int>::iterator it = d2.begin(); it++; d2.erase(it); //200 10 20 print(d2); //按区间删除 d2.erase(d2.begin(), d2.end()); print(d2); //清空 d2.clear(); } int main() { test1(); test2(); test3(); system("pause"); return 0; }
5、数据获取
#include <deque> void test1() { 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++) { //300 200 100 10 20 30 cout << d1[i] << " "; } cout << endl; //通过at访问 for (int i = 0; i < d1.size(); i++) { //300 200 100 10 20 30 cout << d1.at(i) << " "; } cout << endl; //300 cout << "第一个元素:" << d1.front() << endl; //30 cout << "最后一个元素:" << d1.back() << endl; } int main() { test1(); system("pause"); return 0; }
6、排序操作
利用sort进行排序,默认进行升序排序。
#include <deque> #include <algorithm> //标准算法的头文件 void print(const deque<int>& d) { for (deque<int>::const_iterator it = d.begin(); it != d.end(); it++) { cout << *it << " "; } cout << endl; } void test1() { 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); //300 200 100 10 20 30 print(d); //排序 sort(d.begin(), d.end()); cout << "排序后:" << endl; //10 20 30 100 200 300 print(d); } int main() { test1(); system("pause"); return 0; }