1 deque容器基本概念
功能:
deque与vector的区别:
- vector对于头部的插入删除效率低,数据量越大,效率越低(因为vector是单端数据,头插需要将后面的数据向后移动后才能插入,头删的话需要将后面的数据向前移动)
- deque相对而言,对头部的插入删除速度会比vector快
- vector访问元素时的速度会比deque快,这和两者内部实现有关
deque内部工作原理: deque内部有个中控器,维护每段缓冲区中的内容,缓冲区中存放真实数据。中控器维护的是每个缓冲区的地址,使得使用deque时像是一片连续的内存空间。其工作原理决定了它访问数据元素比vector慢的特点。比如读取完一段地址处所指的缓冲区数据后,中控器需要先找到下一段地址所指的缓冲区位置后,才能开始数据的访问。
2 deque容器构造函数
函数原型: 示例:
#include <iostream>
using namespace std;
#include <string>
#include <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());
printDeque(d2);
deque<int>d3(10, 100);
printDeque(d3);
deque<int>d4(d3);
printDeque(d4);
}
int main()
{
test01();
system("pause");
return 0;
}
运行结果:
3 deque容器赋值操作
函数原型: 示例:
#include <iostream>
using namespace std;
#include <string>
#include <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);
deque<int>d3;
d3.assign(d1.begin(), d1.end());
printDeque(d3);
deque<int>d4;
d4.assign(10, 100);
printDeque(d4);
}
int main()
{
test01();
system("pause");
return 0;
}
运行结果:
4 deque容器大小操作
函数原型: 示例:
#include <iostream>
using namespace std;
#include <string>
#include <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的大小为:" << d1.size() << endl;
}
d1.resize(15,1);
printDeque(d1);
d1.resize(5);
printDeque(d1);
}
int main()
{
test01();
system("pause");
return 0;
}
运行结果:
5 deque容器的插入与删除
函数原型: 示例:
#include <iostream>
using namespace std;
#include <string>
#include <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);
d1.pop_back();
printDeque(d1);
d1.pop_front();
printDeque(d1);
}
void test02()
{
deque<int>d1;
d1.push_back(10);
d1.push_back(20);
d1.push_front(100);
d1.push_front(200);
printDeque(d1);
d1.insert(d1.begin(), 1000);
printDeque(d1);
d1.insert(d1.begin(), 2, 10000);
printDeque(d1);
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);
}
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);
printDeque(d1);
d1.clear();
printDeque(d1);
}
int main()
{
test01();
cout << "======================" << endl;
test02();
cout << "======================" << endl;
test03();
system("pause");
return 0;
}
运行结果:
6 deque容器数据存取
函数原型: 示例:
#include <iostream>
using namespace std;
#include <string>
#include <deque>
void test01()
{
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++)
{
cout << d1[i] << " ";
}
cout << endl;
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();
system("pause");
return 0;
}
运行结果:
7 deque容器排序
函数原型: 示例:
#include <iostream>
using namespace std;
#include <string>
#include <deque>
#include <algorithm>
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_back(30);
d1.push_front(100);
d1.push_front(200);
d1.push_front(300);
printDeque(d1);
sort(d1.begin(), d1.end());
cout << "排序后的结果:" << endl;
printDeque(d1);
}
int main()
{
test01();
system("pause");
return 0;
}
运行结果: 注:本文参考b站黑马程序员C++课程
|