【问题标题】:Overloading operator[] for ArrayVector class重载 ArrayVector 类的 operator[]
【发布时间】:2014-11-01 21:45:54
【问题描述】:

您好,我在编写此函数时遇到问题:

int& ArrayVector::operator[](int index)

必须做到以下几点

返回对数组第索引元素的引用

如果索引大于或等于数组的大小, 1.扩大数组覆盖到(index+1) 元素 2. 将元素从旧数组复制到扩展数组 3. 从第 n 到第 (index-1) 的元素填充 0 4. 返回第 index 个元素 还要跟踪数组的大小 到目前为止,这是我的课。我只需要有关此功能的帮助。谢谢!!

class ArrayVector {
private:
  int* data;
  int n;

public:
  ArrayVector() : data(NULL), n(0) { }

  int size() const {
    return n;
  }

  // print content of the array separated by “ “
  void print() {
    if (n == 0)
      return;

    // print from first element to second last element
    for (int i = 0; i < n-1; ++i)
      cout << data[i] << " ";

    // print last element
    cout << data[n-1] << endl;
  }

  // Return reference to index-th element of the array
  //  If index is greater or equal to size of the array,
  //    1. expand the array to cover up to (index+1) 
  //    elements
  //    2. copy elements from old array to expanded array
  //    3. fill 0 for elements from n-th to (index-1)-th
  //    4. return the index-th element
  //  Also keep track the size of the array

  int& operator[](const int index);

};
 


int& ArrayVector::operator[](int index) {
  // TODO
}

【问题讨论】:

  • 好的,那么您尝试了哪些方法,如果遇到困难,您在哪一步遇到了问题?
  • 您的 print() 函数可能看起来像 for(int i = 0; i &lt; n; i++) cout &lt;&lt; data[i] &lt;&lt; " "; ...无需将最后一个元素与 for loop 分开,如果 n 等于 0 那么 for 循环中的语句将永远不会发生所以不需要检查 0 数组大小
  • 哦,既然我们已经有了std::vector,为什么还要实现自己的课程?
  • @Quest 从非常具体的说明中可以明显看出这很可能是某种类型的分配

标签: c++ arrays c++11 vector operator-overloading


【解决方案1】:

如果索引在范围内 - 只需返回元素。 别的 : 将大小为 (n+(n-index+1)) 的新数组分配到临时指针中。 使用 for 循环复制前“n”个值。 将 0 放入新数组的其余部分。 将“n”设置为新大小,删除旧分配的数组。将“数据”设置为指向新数据。 返回元素。

【讨论】:

    【解决方案2】:

    operator[]可以是这样的:

    int& ArrayVector::operator[](int index)
    {
        if(index > 0 && index < n)
            return data[index];                // if index is in range, just return an element
        else{                                  // if not, 
            int *nData = new int[index+1];     // create a new, temporarily array with size index+1
            for(int i = 0; i < n; i++)
                nData[i] = data[i];            // copy data to our new aray
            delete[] data;                     // delete our old array
    
            for(int i = n; i < index+1; i++)
                nData[i] = 0;                  // fill newly created elements with 0
    
            data = nData;                      // set data to point to our new created array
            n = index+1;                       // set new data size
    
            return data[index];                    // return element with that index
        }
    }
    

    【讨论】:

    • 不是真的,给他的指令明确指出,如果向量还不够大,他应该为足够的元素腾出空间。
    • 你几乎明白了。您分配了错误的大小(您实际上应该调整大小以至少适合 index+1 元素,否则如果向量小于 4,vec[5] = 5; std::cout &lt;&lt; vec[5]; 实际上会打印 0)。您也不要删除旧数据。但如果你不只是给提问者完整的答案,我真的更喜欢它,因为这似乎是一项任务
    • @PeterT 感谢您的纠正。我再次更新了我的答案。
    • 几乎if(index &gt; 0 &amp;&amp; index &lt;= n) 应该是if(index &gt; 0 &amp;&amp; index &lt; n)return data[n]; 应该是return data[n-1]; 或者更好,但只是return data[index];
    • @PeterT 再次感谢你:D 我喝醉了,所以这就是它的样子:D 我可能会说谢谢你 tomoerow agan...
    【解决方案3】:

    你可以拥有这个

    int& operator[](const int index){
        if(index>0 && index<n){
           return data[index];
        }
        else if(index>n){
          int *newData = new int[index+1];
          for(int i=0;i<n;i++){
              newData[i]=data[i];
          }
          for(int i=n;i<index+1;i++){
              newData[i]=0;
          }
    
          return newData[index];
        }else{
          return;
        }
    

    };

    【讨论】:

    • 您没有删除旧数据,也没有将“数据”设置为指向新创建的数组。你的函数也需要返回一些东西(你不能只写return;
    猜你喜欢
    • 2012-05-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-12
    相关资源
    最近更新 更多