您现在的位置: 主页 > 电子技术 > EDA > 使用C++ 实现缓存容量增加 -
本文所属标签:
为本文创立个标签吧:

使用C++ 实现缓存容量增加 -

来源: 网络用户发布,如有版权联系网管删除 2018-08-05 

[导读]当你在某个缓存中存储数据时,常常需要在运行时调整该缓存的大小,以便能容纳更多的数据。
下面是一个增加初始缓存大小的例子:
view plaincopy to clipboardprint?
// console.cpp : Defines the entry point for t

当你在某个缓存中存储数据时,常常需要在运行时调整该缓存的大小,以便能容纳更多的数据。

下面是一个增加初始缓存大小的例子:

view plaincopy to clipboardprint?

// console.cpp : Defines the entry point for the console application.

//

#include "stdafx.h"

#include <iostream>

#include <algorithm>

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;

}



来源:博士0次

本文引用地址: http://www.21ic.com/app/eda/201806/769081.htm



              查看评论 回复



嵌入式交流网主页 > 电子技术 > EDA > 使用C++ 实现缓存容量增加 -
 

"使用C++ 实现缓存容量增加 -"的相关文章

网站地图

围观()