deque是双端队列,其表象看起来是可以双端扩充,但实际上是通过内存映射管理来营造可以双端扩充的假象,如图所示

比如,用户将最左端的buff用光时,map会自动向左扩充,继续申请并映射一个新的buff,右端同理。

例程

#include<stdexcept>
#include<string>
#include<cstdlib>
#include<stdio.h>
#include<algorithm>//sort,qsort
#include<iostream>
#include<ctime>
#include<deque>
using namespace std; long get_a_target_long()
{
long target = 0;
cout<<"target(0~"<<RAND_MAX<<"):";
cin>>target;
return target;
}
string get_a_target_string()
{
long target = 0;
char buf[10];
cout<<"target(0~"<<RAND_MAX<<"):";
cin>>target;
snprintf(buf, 10, "%ld", target);
return string(buf);
}
int compareLongs(const void* a, const void* b)
{
return (*(long*)a - *(long*)b);
} int compareStrings(const void *a, const void *b)
{
if(*(string*)a > *(string*)b)
return 1;
else if(*(string*)a < *(string*)b)
return -1;
else
return 0;
}
void test_deque(long& value)
{
cout << "\ntest_deque().......... \n"; deque<string> c;
char buf[10]; clock_t timeStart = clock();
for(long i=0; i< value; ++i)
{
try
{
snprintf(buf, 10, "%d", rand());
c.push_front(string(buf));
}
catch(exception& p)
{
cout << "i=" << i << " " << p.what() << endl;
abort();
}
}
cout << "milli-seconds : " << (clock()-timeStart) << endl;
cout << "deque.size()= " << c.size() << endl;
cout << "deque.front()= " << c.front() << endl;
cout << "deque.back()= " << c.back() << endl;
cout << "deque.max_size()= " << c.max_size() << endl; string target = get_a_target_string();
timeStart = clock();
auto pItem = ::find(c.begin(), c.end(), target);// 显示指明使用#include<algorithm>中的find() or sort()
cout << "std::find(), milli-seconds : " << (clock()-timeStart) << endl; if (pItem != c.end())
cout << "found, " << *pItem << endl;
else
cout << "not found! " << endl; timeStart = clock();
::sort(c.begin(), c.end()); // 显示指明使用#include<algorithm>中的find() or sort()
cout << "c.sort(), milli-seconds : " << (clock()-timeStart) << endl;
c.clear();
}
int main()
{
long int value;
cout<<"how many elements:";
cin>>value;
test_deque(value);
return 0;
}

运行结果

容器的分类与各种测试(三)——deque的更多相关文章

  1. 容器之分类与各种测试(三)——stack

    stack是栈,其实现也是使用了双端队列(只要不用双端队列的一端,仅用单端数据进出即完成单端队列的功能),由于queue和stack的实现均是使用deque,没有自己的数据结构和算法,所以这俩也被称为 ...

  2. 容器之分类与各种测试(三)——queue

    queue是单端队列,但是在其实现上是使用的双端队列,所以在queue的实现上多用的是deque的方法.(只要用双端队列的一端只出数据,另一端只进数据即可从功能上实现单端队列)如下图 例程 #incl ...

  3. 容器之分类与各种测试(三)——slist的用法

    slist和forward_list的不同之处在于其所在的库 使用slist需要包含 #include<ext\list> 而使用forward_list则需要包含 #include< ...

  4. 容器之分类与各种测试(三)——forward_list的用法

    forward_list是C++11规定的新标准单项链表,slist是g++以前的规定的单项链表 例程 #include<stdexcept> #include<string> ...

  5. 容器之分类与各种测试(三)——list部分用法

    list是一个双向链表 例程 #include<stdexcept> #include<memory.h> #include<string> #include< ...

  6. 容器的分类与各种测试(二)——vector部分用法

    向量 vector 是一种对象实体, 能够容纳许多其他类型相同的元素, 因此又被称为容器. 与string相同, vector 同属于STL(Standard Template Library, 标准 ...

  7. 容器之分类与各种测试(四)——unordered-multiset

    unordered-multiset是不定序关联式容器,其底部是通过哈希表实现功能. (ps:黑色框就是bucket,白色框即为bucket上挂载的元素) 为了提高查找效率,bucket(篮子)的数量 ...

  8. 容器之分类与各种测试(四)——multiset

    multiset是可重复关键字的关联式容器,其与序列式容器相比最大的优势在于其查找效率相当高.(牺牲空间换取时间段) 例程 #include<stdexcept> #include< ...

  9. 容器之分类与各种测试(四)——unordered_set和unordered_map

    关于set和map的区别前面已经说过,这里仅是用hashtable将其实现,所以不做过多说明,直接看程序 unordered_set #include<stdexcept> #includ ...

随机推荐

  1. Java RMI学习与解读(三)

    Java RMI学习与解读(三) 写在前面 接下来这篇就是最感兴趣的Attack RMI部分了. 前面也说过,RMI的通信过程会用到反序列化,那么针对于RMI的三个角色: Server/Regisrt ...

  2. 关于linux7.x系列下的 systemd 的理解

    历史上Linux的启动一直采用init进程,下面的命令用来启动服务. $ sudo /etc/init.d/apache2 start #或者 $ service apache2 start 这种方法 ...

  3. float32 和 float64

    float32 和 float64 Go语言中提供了两种精度的浮点数 float32 和 float64. float32,也即我们常说的单精度,存储占用4个字节,也即4*8=32位,其中1位用来符号 ...

  4. @PostConstruct和static静态块初始化的区别

    static blocks are invoked when the class is being initialized, after it is loaded. The dependencies ...

  5. CentOS 设置网络及安装 ifconfig

    centos使用yum报错"Could not resolve host: mirrorlist.centos.org; 未知的错误" 先用NetworkManager包的nmcl ...

  6. IDEA 运行maven工程报错:No goals have been specified for this build.....解决办法

    出现这种错误可以在pom.xml里配置, 找到<build>标签在下面<plugins>标签上面加上<defaultGoal>compile</default ...

  7. 菜鸡的Java笔记 第十六 - java 引用传递

    referenceDelivery    引用传递是整个java 的精髓,也是所有初学者最难学的地方        引用的本质:同一块堆内存可以被不同的栈内存所指向    下面通过三道程序来进行引用传 ...

  8. 如何设计一个高内聚低耦合的模块——MegEngine 中自定义 Op 系统的实践经验

    作者:褚超群 | 旷视科技 MegEngine 架构师 背景介绍 在算法研究的过程中,算法同学们可能经常会尝试定义各种新的神经网络层(neural network layer),比如 Layer No ...

  9. [bzoj4971]记忆中的背包

    为了使得方案的形式较为单一,不妨强制物品体积为1或$\ge \lceil\frac{w}{2}\rceil$,那么假设最终有$x$个1且$\ge \lceil\frac{w}{2}\rceil$的物品 ...

  10. [bzoj5343]混合果汁

    二分枚举答案,问题转化为计算至少取到一定体积,价格最少是多少,显然是贪心取最小,用线段树维护,然后因为要判断答案,所以可持久化一下即可. 1 #include<bits/stdc++.h> ...