
使用C++ 实现缓存容量增加
当你在某个缓存中存储数据时,常常需要在运行时调整该缓存的大小,以便能容纳更多的数据。
下面是一个增加初始缓存大小的例子:
view plaincopy to clipboardprint?
// console.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include
#include
using namespace std;
int reallocate(int* &p, int& size)
{
size*=2; // double the array''s size with each reallocation
int * temp = new int[size];
copy(p, p+(size/2), temp);
delete [] p; // release original, smaller buffer
p=temp; // reassign p to the newly allocated buffer
return 1;
}
int main(void)
{
int size=2; // 初始化数组大小;在运行时调整。
int *p = new int[size];
int isbn;
for(int n=0; ;++n)
{
cout<< "enter an ISBN; press 0 to stop ";
cin>>isbn;
if (isbn==0)
break;
if (n==size) // 数组是否到达上限?
reallocate(p, size);
p[n]=isbn; // 将元素插入扩容的数组
}
delete [] p; // 不要忘了这一步!
return 0;
}
2017年计算机二级C++辅导编程:使用C++ 实现缓存容量增加.doc正在阅读:
2017年计算机二级C++辅导编程:使用C++实现缓存容量增加08-20
生命在于运动作文01-10
二之国交错世界天空岛怎么上分?(图文)06-08
2017年湖北标准员合格证书办理时间(已开始办理)10-30
建筑公司材料采购员年终总结,公司材料采购员工作年终总结04-27
2023年5月贵州商务英语考试时间、科目、级别及内容[5月13日、20日、27日]03-02
白鳍豚琪琪的遗书作文800字08-02
难忘的第一次作文500字09-13
六年级写人作文:向您致敬,干警爸爸_爸爸作文800字01-05
电机厂生产实习报告3000字02-10