2017年计算机二级office操作题,2017年计算机二级C++辅导编程:使用C++实现缓存容量增加

副标题:2017年计算机二级C++辅导编程:使用C++实现缓存容量增加

时间:2023-08-20 06:02:01 阅读: 最新文章 文档下载
说明:文章内容仅供预览,部分内容可能不全。下载后的文档,内容与下面显示的完全一致。下载之前请确认下面内容是否您想要的,是否完整无缺。


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

本文来源:https://www.wddqw.com/aGvn.html