Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/154.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 动态数组调整函数大小问题_C++_Arrays_Pointers_Delete Operator - Fatal编程技术网

C++ 动态数组调整函数大小问题

C++ 动态数组调整函数大小问题,c++,arrays,pointers,delete-operator,C++,Arrays,Pointers,Delete Operator,注意:我知道只使用STL向量会更容易,但是,对于我所在的编程类,我们需要编写自己的动态数组模板类,因为我们的教授显然喜欢让我们重新发明轮子 无论如何,我为我的动态数组模板类创建了一个resize函数,如下所示。注意,私有成员变量是T*arr、unsigned used和unsigned cap template <class T> void darray<T>::resize(unsigned size) { if (size > cap) {

注意:我知道只使用STL向量会更容易,但是,对于我所在的编程类,我们需要编写自己的动态数组模板类,因为我们的教授显然喜欢让我们重新发明轮子

无论如何,我为我的动态数组模板类创建了一个resize函数,如下所示。注意,私有成员变量是T*arr、unsigned used和unsigned cap

template <class T>
void darray<T>::resize(unsigned size)
{
    if (size > cap)
    {
        T* temp_arr = new T[size];

        for (int count = 0; count < used; ++count)
            temp_arr[count] = arr[count];

        for (int count = used; count < size; ++count)
            temp_arr[count] = T();

        delete []arr;
        arr = temp_arr;
        cap = size;
        used = size;
    }

    if (size < cap)
    {
        used = size;
    }
}
}

分配操作员:

template <class T>
darray<T>& darray<T>::operator= (const darray& right_operand)
{
    delete[] arr;
    arr = new T[right_operand.capacity()];
    used = right_operand.size();
    cap = right_operand.capacity();

    for (int count = 0; count < used; ++count)
        arr[count] = right_operand[count];
    return *this;
}
模板
darray和darray::运算符=(常量darray和右操作数)
{
删除[]arr;
arr=新的T[右操作数.容量()];
used=右操作数.size();
cap=右操作数。容量();
for(int count=0;count
析构函数:

template <class T>
darray<T>::~darray()
{
    delete[] arr;
}
模板
达瑞::~darray()
{
删除[]arr;
}
有人要求使用push_-back功能,这里也有:

template <class T>
void darray<T>::push_back(const T& input)
{
    if ((used + 1) > cap)
    {
    resize(cap * 2);
    arr[used + 1] = input;
    ++used;
    }

    else
    {
        arr[used] = input;
        ++used;
    }
}
模板
无效darray::推回(常量T和输入)
{
如果((使用+1)>上限)
{
调整大小(第2章);
arr[used+1]=输入;
++使用;
}
其他的
{
arr[使用]=输入;
++使用;
}
}

我将使用下一个实现。我假设“cap”成员表示向量的容量,“used”表示数组中元素的数量

template <class T>
void darray<T>::resize(unsigned size)
{
  if (size > cap)
  {
    cap = size * 2;
    T* temp_arr = new T[cap];

    for (int count = 0; count < used; ++count)
        temp_arr[count] = arr[count];

    delete [] arr;
    arr = temp_arr;
  }   

  // zero members if size was decreased
  if (size < used)
  {  
     for (int count = size; count < used; ++count)
          arr[count] = T();
  }

  used = size;
}
模板
void darray::调整大小(无符号大小)
{
如果(大小>上限)
{
盖=尺寸*2;
T*temp_arr=新的T[cap];
for(int count=0;count
优点:您不需要在每次使用大量元素调整大小时重新分配整个阵列


缺点:当您不需要时,您会花费额外的空间。

您的
调整大小
功能会增加
使用的
。在
推回
中访问
arr[used+1]
时,访问的数组位置无效

您应该添加另一个函数,该函数类似于
resize
,但只更改阵列的容量,而不更改存储的对象计数。(即,它不会增加所使用的
)。此函数应通过
push_back
调用。(正如您在问题中提到的
std::vector
,请参见和之间的区别。)


另外:数组索引是从零开始的。不要在位置
used+1
插入,但在位置
used

上,您缺少一个;在第二个星期,教授做出了一个很好的选择,因为创建一个有效的实现显然不是一件小事;)不应该使用
T()
be
new T()
?@user3559437我们能看到用于所讨论对象的构造函数吗?实际上,not setting used=size修复了问题。谢谢你,维塔斯!
template <class T>
void darray<T>::resize(unsigned size)
{
  if (size > cap)
  {
    cap = size * 2;
    T* temp_arr = new T[cap];

    for (int count = 0; count < used; ++count)
        temp_arr[count] = arr[count];

    delete [] arr;
    arr = temp_arr;
  }   

  // zero members if size was decreased
  if (size < used)
  {  
     for (int count = size; count < used; ++count)
          arr[count] = T();
  }

  used = size;
}