首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

新年快乐!求解咯!该如何解决

2012-03-20 
新年快乐!求解咯!Goal:- dynamic memory managementTask:Implement an Occupied class which can store an

新年快乐!求解咯!
Goal:
- dynamic memory management

Task:
Implement an Occupied class which can store an undefined number of different floats consecutive in memory. The constructor accepts an integer as initial size of the container. If no value is specified, a size of 5 is assumed.
The method void add_element(float) adds an element to the container if it is not yet stored and adapts the size if needed.
The method bool remove_element(int index) removes the element at position index, if available and shifts all remaining elements. Returns true if element is removed.
The method bool get_element(int index, float & result) returns the specific element, if available, in result and use the return value to flag failure/succes.
The method Show() which should show info about the container like:
Size of container : 3
* the 1st element = 1.26345
* the 2nd element = -0.25
* the 3rd element = 444444444.01

What happens with following code?
Occupied o = 5; // how can we let the compiler to refuse it?
Check your memory management by instantiating a very large container, e.g. Occupied large(99999999); in a local scope and stopping the program both inside and directly outside this scope. Use "Task Manager" to visualise your memory usage.
Create a suitable application which test all the implemented functionaly.
 
谁能帮我翻译一下啊~~~~~~~~求大神了 T_T

[解决办法]
哥不太相信那些自动翻译,手工给你翻译如下:

目标:
- 动态内存管理

任务:
实现一个Occupied类,该类可以连续内存中,存储任意数量的不同float数据,其构造函数接受一个整形参数,用作容器的初始大小,如果没有给定初始值,那么就假定其初始大小是5.

如果一个元素在容器中尚未存在,方法void add_element(float)将向容器中增加该元素,同时,如果必要,需调整容器的大小。
如果位于index的元素存在,方法bool remove_element(int index)将删除该元素,并将其后的元素依次前移。如果元素被成功删除,该方法返回true。
方法bool get_element(int index, float & result)返回位置为index的元素,如果该元素存在,那么返回结果将保存在result中。同时根据这个返回值来标志函数的失败/成功。
方法Show()应该用类似下面的形式输出容器的内容:
容器的大小 : 3
* 第一个元素 = 1.26345
* 第二个元素 = -0.25
* 第三个元素 = 444444444.01

下面的代码会发生什么事情呢?
Occupied o = 5; // 我们怎样让编译器拒绝此种行为?
实例化一个非常大的容器来检验内存管理,比如在本地范围Occupied large(99999999);然后在分别在本地范围内、外终止程序运行,并用“任务管理器”查看内存的使用情况。

创建一个合适的程序,用以测试所有实现的功能。

热点排行