我的矢量插入代码有什么问题?

时间:2019-02-21 00:34:00

标签: vector insert

我目前正在实验室中,我必须创建自己的向量类。我有一个单独的类来测试方法以确保它们有效。

我的insert方法遇到问题,我看不出问题是什么。我给我的朋友看了,他说他的代码是完全一样的,他的作品还不错。

我的代码:

// Default ctor.
  // Initialize an empty Array.
  // This method is complete, and does NOT need modification. 
  // Remember to use a _member initialization list_ for each ctor,
  //   like I have below for the default ctor. 
  Array ()
    : m_size (0),
      m_capacity (0),
      m_array (nullptr)
  {
  }

  // Size ctor.
  // Initialize an Array of size "pSize", with each element
  //   set to "value". 
  explicit Array (size_t pSize, const T& value = T ())
    : m_size (pSize),
      m_capacity (pSize), 
      m_array (new T[m_capacity])
  {
    fill (begin (), end (), value);
  }

  // TODO!
  // Range ctor.
  // Initialize an Array from the range [first, last).
  // "first" and "last" must be Array iterators or pointers
  //   into a primitive array. 
  Array (const_iterator first, const_iterator last)
    : m_size (distance(first, last)),
      m_capacity (m_size),
      m_array (new T[m_capacity])
  {
    for(size_t i = 0; i < distance(first, last); ++i)
    {
        m_array[i] = *first++; //copy(first, last, m_array);
    }
  }

  // TODO!
  // Copy ctor.
  // Initialize this object from "a".
  Array (const Array& a)
    : m_size (a.size()),
      m_capacity (a.capacity()),
      m_array (new T[m_capacity])
  {
    copy(a.begin(), a.end(), m_array);
  }

  // TODO!
  // Destructor.
  // Release allocated memory.
  ~Array ()
  {
    delete[] m_array;
  }  

  // TODO!
  // Assignment operator.
  // Assign "a" to this object.
  //   Be careful to check for self-assignment.
  Array&
  operator= (const Array& a)
  {
    if(this != &a)
    {
        m_size = a.size();
        m_capacity = a.capacity();
        *m_array = *(a.m_array); //delete array and assign new one?
    }
    return *this;
  }

  // Return the size.
  size_t
  size () const
  {
    return m_size;
  }

  // TODO!
  // Return true if this Array is empty, false o/w.
  bool
  empty () const
  {
    return m_size == 0;
  }

  // TODO!
  // Return the capacity.
  size_t
  capacity () const
  {
    return m_capacity;
  }

  // TODO!
  // Return the element at position "index".
  T&
  operator[] (size_t index)
  {
    return m_array[index];
  }

  // TODO!
  const T&
  operator[] (size_t index) const
  {
    return m_array[index];
  }

  // TODO!
  // Insert an element at the back.
  void
  push_back (const T& item)
  {
    insert(end(), item);
  }

  // TODO!
  // Erase the element at the back.
  void
  pop_back ()
  {
    erase(end());
  }

  // Reserve capacity for "space" elements.
  // "space" must be  greater than capacity. 
  //   If not, leave the capacity unchanged.
  // "size" must remain unchanged.
  void
  reserve (size_t space)
  {
    if (space > capacity ())
    {
      T* array = new T[space];
      copy (begin (), end (), array);
      delete[] m_array;
      m_array = array;
      m_capacity = space;
    }
  }

  // TODO!
  // Change the size to be "newSize".
  // If "newSize" is less than "size",
  //   erase the last elements.
  // If "newSize" is more than "size", 
  //   insert "value"-s at the end.
  void
  resize (size_t newSize, const T& value = T ())
  {
    if(m_size < newSize)
    {
        //T* end = end();
        size_t diff = newSize - m_size;
        reserve(newSize);
        fill(end(), end() + diff, value);
        //fill(end, end(), value);//end() does not go to end of cap
        //delete[] end;
    }

    m_size = newSize;
  }

  // TODO!
  // Insert "item" before "pos", and return iterator pointing to "item".
  // If the capacity is insufficient, DOUBLE it.
  //   If the capacity is 0, increase it to 1.
  // NOTE: If a reallocation occurs, "pos" will be invalidated!
  iterator
  insert (iterator pos, const T& item)
  {
    //reallocate space if needed
    if(m_capacity == m_size)
    {
        size_t index = pos - m_array; 

        if(m_capacity == 0)
            reserve(1);
        else
            reserve(m_capacity *= 2);

        pos = m_array + index;
    }

    //shift elements over 1
    for(iterator i = end(); i >= pos; --i)//std::move?
        *(i) = *(i - 1);

    //insert
    *pos = item;

    ++m_size;
    return pos;
  }

  // TODO!
  // Remove element at "pos", and return an iterator
  //   referencing the next element.
  iterator
  erase  (iterator pos)
  {
    size_t diff = distance(pos, m_array); 
    iterator it = &m_array[diff];
    it -> ~T();

    --m_size;
    return it;
  }

  // TODO!
  // Return iterator pointing to the first element.
  iterator
  begin ()
  {
    return m_array;
  }

  // TODO!
  const_iterator
  begin () const
  {
    return m_array;
  }

  // TODO!
  // Return iterator pointing one beyond the last element.
  iterator
  end ()
  {
    return m_array + m_size;
  }

  // TODO!
  const_iterator
  end () const
  {
    return m_array + m_size;
  }

private:
  // Stores the number of elements in the Array. 
  size_t m_size;
  // Stores the capacity of the Array, which must be at least "m_size".
  size_t m_capacity;
  // Stores a pointer to the first element in the Array. 
  T*     m_array;
};

在测试器类中,我做了一个for循环,以相反的顺序插入结果。这是失败的情况和结果:

for (int i = 0; i < 10; ++i)
    A.insert (A.begin (), i);

预期:“ [9 8 7 6 5 4 3 2 1 0]” 实际:“ [9 8 7 6 5 4 3 0 540614747 540483640 540352566]”

似乎跳过了2和1,超出了范围,我不知道为什么

有些其他方法可能是错误的,因为我没有在测试中做得更好,所以请告诉我是否有任何发现。

顺便说一句,没有在他们上面说TODO的方法意味着我的教授为我们做了那些。

谢谢=)

0 个答案:

没有答案