2011年计算机二级C++辅导实例编程(26)

2014-10-19 00:09:13 · 作者: · 浏览: 560

  使用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