【问题标题】:How to implement resize() to change capacity of dynamic member data in c++如何在 C++ 中实现 resize() 来改变动态成员数据的容量
【发布时间】:2018-03-05 23:09:22
【问题描述】:

我需要实现函数resize():

 void IntSet::resize(int new_capacity)
 {
     if (new_capacity < used)
         new_capacity = used;
     if (used == 0)
         new_capacity = 1;

     capacity = new_capacity;

     int * newData = new int[capacity];

     for (int i = 0; i < used; ++i)
         newData[i] = data[i];

     delete [] data;
     data = newData;
}

函数内部:

 IntSet IntSet::unionWith(const IntSet& otherIntSet) const
 {
       IntSet unionSet = otherIntSet;

       for (int i = 0; i < used; i++)
       {
           if (unionSet.contains(data[i]))
              unionSet.add(data[i]);
       }
       return unionSet;
 }

还有这个:(注意:我已经在 add() 函数中找到了它,但我认为它不正确)

 bool IntSet::add(int anInt)
 {
     if (contains(anInt) == false)
     {
        if (used >= capacity)
           resize(used++);

        data[used++] = anInt;

        return true;
     }
     return false;
 }

程序正确编译没有错误,但它确实给了我一个分段错误

的错误

注意:主要是我需要帮助来学习如何使用 resize 函数来调整动态成员数据的 容量。另外,我知道向量在这种情况下会有所帮助,但我们还不能使用向量

这是教授的特殊要求

>Special Requirement (You will lose points if you don't observe this.) <br/>

>When calling resize *(while implementing some of the member functions)* to 
>increase the capacity of the dynamic arrays, use the following resizing 
>rule (unless the new capacity has to be something else higher as dictated 
>by other >overriding factors): <br/>
>
>*"new capacity" is "roughly 1.5 x old capacity" and at least "old capacity 
> + 1".* <br/>
>
>The latter *(at least " old capacity + 1 ")* is a simple way to take care 
>of the subtle case where " 1.5 x old capacity " evaluates (with truncation) 
>to the >same as "old capacity". <br/>

【问题讨论】:

  • 不相关的注释:if (used &gt;= capacity) resize(used++); 如果used 大于capacity 你已经输了。东西已经被砸碎了。 ““新容量”是“大约 1.5 x 旧容量”并且至少“旧容量 > + 1”。 1.5 比 1 好很多。否则,您会不断调整大小。
  • 那么如何检查是否需要调整大小?或者我应该自动调整大小,除非我只做 if (used == capacity) 和 resize ((1.5 * capacity) + 1) ??我对此有点困惑

标签: c++ class dynamic-memory-allocation dynamic-class


【解决方案1】:

当您因为addresize 时,您将used 增加两次。

bool IntSet::add(int anInt)
{
    if (contains(anInt) == false)
    {
       if (used >= capacity)
          resize(used++);  // Here And this is a post increment. 
                           // resize will be called with used before the increment
                           // so you will wind up asking for a buffer the same size.

       data[used++] = anInt; // and here. 

       return true;
    }
    return false;
}  

所以没有任何东西被使用。您跳过一个空格并在之后写入空格。加上resize(used++); 没有要求更多空间,所以你实际上在分配的存储之外写了两个位置,这可能会触发段错误。

解决方案

您不想在resize(used++); 处增加任何内容。您想在capacity 中添加一个,但不增加它,所以

resize(capacity +1);

看起来不错。然而,指令要求的更像是:

int newcap = capacity * 1.5;
if (newcap == capacity) // newcap didn't change. eg: 1*1.5 = 1
{
    newcap ++;
}
resize(newcap);

虽然这是蛮力。有更聪明的方法可以做到这一点。

【讨论】:

  • 谢谢!我实际上最终使用了它并且它有效。 resize(int(1.25 * capacity) + 1);
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-11-15
  • 1970-01-01
相关资源
最近更新 更多