使用C++ 实现缓存容量增加 当你在某个缓存中存储数据时,常常需要在运行时调整该缓存的大小,以便能容纳更多的数据。
: {, k$ W4 l# S 下面是一个增加初始缓存大小的例子:+ E. K% X" ~9 T( t6 {7 l
view plaincopy to clipboardprint?
' |% _7 ?" T4 D // console.cpp : Defines the entry point for the console application.7 l3 i: `8 _7 u6 @* F+ l4 g( G
// Z, U/ Q& o% _& F. T3 }% ~
#include "stdafx.h"' E B* ^% U- L7 F4 O; M, X
#include' x: ^8 H1 Z. ]4 @3 L% e2 [
#include# |1 N" \$ k2 K
using namespace std;
L9 R0 ~, A, p; u+ C int reallocate(int* &p, int& size)' s7 B8 M$ j4 U4 ^$ H. W( t; W
{
: S+ o! T6 N# G1 g& ` size*=2; // double the array''s size with each reallocation
; L# o) U' j% s) f# d3 ~+ k int * temp = new int[size];
* U" {" R0 ~0 x0 _! \! o% W0 f copy(p, p+(size/2), temp);0 a6 `, V8 A7 P
delete [] p; // release original, smaller buffer |