灵活的可调整大小的数组c ++ [英] flexible resizable array c++

查看:108
本文介绍了灵活的可调整大小的数组c ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的任务是创建一个实现安全可调整大小数组的类,该数组可以包含任何单一类型的数据。



使用模板以便存储的数据数组可以是任何类型

提供默认(没有参数构造函数将数组设置为某个合理的默认初始大小

提供另一个带有一个int参数的构造函数允许程序员指定原始大小

所以这些都是可能的声明(只是一个例子)

基本上我需要知道如果我在正确的轨道上,如果不是我应该怎么做。



My assignment is to create a class that implements a safe resizable array, which can contain data of any single type.

Use templates so that the data stored in the arrays can be of any type
Provide a "default" (no parameter constructor that sets the array to some reasonable default initial size
Provide another constructor with one int parameter that allows the programmer to specify the original size
So these would all be possible declarations (just an example)
basically what I need to know if I am on the right track, and if not what should I do.

array<int> A;
array<string> B,C;
array<double> D(100);

template<typename stuff> //my template
class safearray
{
public:
    stuff data[];
    safearray def_size() // default size
        {int size = 10; return size;}

    //constructor which the user can later use to set  the array's size themselves
    safearray new_size(new_int) 
        {data = new int [newsize]; size = newsize; }

推荐答案

是的,看起来你走在了正确的轨道上。但是,你知道,构造函数应该具有相同的类名。
Yes, it looks you are on the right track. However, you know, constructors should have the same name of the class.


正如CPallini所说,你几乎走在正确的轨道上。只有几个小问题需要纠正:



您将内部数组声明为

As CPallini said, you are almost on the right track. There are just a couple of small issues that need to be corrected:

You declared your internal array as
stuff data[];



这在这里不起作用。在C ++中,没有可变大小的数组。相反,您应该使用指向已分配空间的指针,例如:


And that doesn't work here. In C++ there is no such thing as a variable size array. Instead, you should use a pointer to your allocated space, for example:

stuff* pData;
...
safearray (int newSize)
    {pData = new int[newSize];}





然后命名约定存在问题。你调用单元格类型 stuff ,这有点误导。 stuff 对于保存数据的变量来说是一个好名字,但对于数据类型本身则不是。例如,stuffType会传达这种意义。但是大多数开发人员使用单个大写字母或非常短的大写字母来表示他们的模板类型,例如T或TYPE。



您的函数def_size有错误返回类型。如果它有任何返回类型size_t(或最好是int)。由于此函数只是设置的默认大小,因此根本不需要任何返回类型。我会将其声明为



Then there is an issue in naming conventions. You call the cell type stuff, which is kind of misleading. stuff would be a good name for a variable that holds your data, but not for the data type itself. For example stuffType would convey that meaning. But most developers use single capital letters or very short capital names to denote their template types, for example "T" or "TYPE".

Your function def_size has the wrong return type. If any it should have return type size_t (or int at best). As this function just sets the default size, it doesn't need any return type at all. I would declare it as

void def_size ()





但该函数还有另一个错误。它为size成员变量赋值,但不为数组分配内存。因此,您不知道size是表示已分配的数组的大小还是下次应分配的大小。有两种方法摆脱这种困境:



(a)始终先分配,然后将大小分配给您的大小变量。例如:



But there is yet another error in that function. It assigns a value to the size member variable, but it doesn't allocate the memory for array. Hence, you don't know whether size denotes the size of the array that you have already allocated or the size that should be allocated on the next occasion. There are two ways out of that dilemma:

(a) Always allocate first and then assign the size to your size variable. For exmaple:

void set_default_size()
    {allocate (10);}
void allocate (size_t newSize)
{
    pData = new int[newSize];
    size = newSize;
    // there is still a BIG error in this function, which
    // we talk about later
}





(b)使用两个变量,一个用于当前分配的大小,另一个用于默认大小:



(b) Use two variables, one for the currently allocated size and one for the default size:

private:
    size_t m_size;       // size that we have currently allocated
    size_t m_defaultSize; // size to use as default value





如您所见,我将大小名称加上m_前缀。这是为了帮助你的代码读者理解这些是成员变量,这并不总是很明显。



最后,分配函数仍然存在问题,以上。我将该代码分离为一个函数,因为它比使用new关键字稍微复杂一点。如果用户多次调用此函数怎么办?旧的存储区域丢失,并且存储到目前为止存储的内容。这称为内存泄漏。你将越来越多地耗尽堆存储。因此,您必须检查是否已为阵列分配了内存。如果是,并且现有内存小于请求的内存,则必须分配一个新的更大的存储区域,然后将旧内容复制到该新区域,最后使用delete []处理旧存储区域。我没有给你代码,因为这是你的功课和一个很好的exersize。



As you see, I prefixed the size names with "m_". That is to help readers of your code to understand that these are member variables, which is not always obvious.

Finally, there is still a problem in the allocate function, above. I separated that code into a function, because it is a little more complex than just using the new keyword. What if the user calls this function several times? The old memory area is lost and with it the contents stored so far. This is called a memory leak. You will be exhausting the heap storage more and more. So you have to check if you have already allocated memory for your array. If yes, and the existing memory is smaller than the requested one, you will have to allocate a new and bigger storage area, then copy the old contents over to that new area and finally dispose of the old storage area by using delete[]. I don't give you the code for that, because this is your homework and a good exersize.


这篇关于灵活的可调整大小的数组c ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆