deque是连续空间(至少在逻辑上看是这样的;它实际是由一段一段的定量连续空间构成。一旦有必要在deque的前端后尾端增加新空间,变配置定量一段连续的空间,串接在整个deque的头端或者尾端。deque最大的任务,便是在这项分段的定量的连续空间上,维护其整体连续的假象,并提供随机存取的接口
deque采用的是块所谓的map(注意,不是STL的map容器)作为主控。这里的map是一块连续空间,其中每个元素最为一个节点,都是指针,指向另一段(较大的)连续线性空间,称为缓存区。缓存区才是deque的存储空间主题,STL允许我们制定存储空间的大小,默认是512bytes缓冲区。
如下图所示
deque使用方法如下:
#include<deque>
using std::deque;
using std::sort;
namespace stl_deque
{
void test_deque(long& value)
{
deque<string> c;
char buf[10];
clock_t timeStart = clock();
for (long i = 0; i < value; ++i)
{
try {
sprintf(buf, "%d", rand());
c.push_back(string(buf));
}
catch (exception& p)
{
cout << "i=" << i << " " << p.what() << endl;
abort();
}
}
cout << "mill-seconds:" << clock() - timeStart << endl;
cout << "deque.size" << c.size() << endl;
cout << "deque.max_size()" << c.max_size() << endl;
cout << "deque.front" << c.front().c_str() << endl;
cout << "deque.back" << c.back().c_str() << endl;
string target = get_a_target_string();
timeStart = clock();
auto pItem = std::find(c.begin(), c.end(), target);
cout << "find(),mill-seconds:" << clock() - timeStart << endl;
if (pItem != c.end())
cout << "found," << (*pItem).c_str() << endl;
else
cout << "not found" << endl;
timeStart = clock();
// c.sort();
sort(c.begin(), c.end(), compareStrings);
cout << "c.sort,mill-seconds:" << (clock() - timeStart) << endl;
}
}