2015-09-27 51 views
0

我的代码不断崩溃,我相信这是因为当我在Vector类的插入函数中向后循环时,我将迭代器递减到原始指针变量的后面。这里的插入功能:如果增加分配给指针的迭代器,指针是否会保存原始内存位置?

iterator insert(iterator & iter, const Object& obj){ 
     if (theSize >= theCapacity){ 
      resize(theSize+1); 
      int *p = iter; 
      for (iter; iter != this->end(); iter++){ 
       //cout << "test1" << endl; 
      } 
      for (iter; iter != p; iter--){ 
       *(iter-1) = *(iter-2); 
       cout << "test1" << endl; 
       //cout << *(iter - 2) << endl; 
       //cout << *(iter - 1) << endl; 
      } 
     } 
     else{ 
      int *p = iter; 
      for (iter; iter != this->end(); iter++){ 
       cout << "test" << endl; 
      } 
      for (iter; iter != p; iter--){ 
       *(iter-1) = (*iter-2); 
      } 

     } 
     *iter = obj; 
     cout << theSize << endl << theCapacity << endl; 
     //theSize++; 
     return this->begin(); 
    } 

插入功能的目标是将对象插入到迭代器位置,并在我的代码,我要确保矢量数组足够长的时间,然后我在移动中的每个对象数组到下一个索引空间;然后我将对象插入迭代器指定的位置。

也是整个Vector类是这样的:

#ifndef VECTOR_H 
#define VECTOR_H 

#include <algorithm> 
#include <iostream> 

template <typename Object> 
class Vector 
{ 
public: 
    explicit Vector(int initSize = 0) 
     : theSize{ initSize }, theCapacity{ initSize + SPARE_CAPACITY } 
    { 
     objects = new Object[theCapacity]; 
    } 

    Vector(const Vector & rhs) 
     : theSize{ rhs.theSize }, theCapacity{ rhs.theCapacity }, objects{ nullptr } 
    { 
     objects = new Object[theCapacity]; 
     for (int k = 0; k < theSize; ++k) 
      objects[k] = rhs.objects[k]; 
    } 

    Vector & operator= (const Vector & rhs) 
    { 
     Vector copy = rhs; 
     std::swap(*this, copy); 
     return *this; 
    } 

    ~Vector() 
    { 
     delete[] objects; 
    } 

    Vector(Vector && rhs) 
     : theSize{ rhs.theSize }, theCapacity{ rhs.theCapacity }, objects{ rhs.objects } 
    { 
     rhs.objects = nullptr; 
     rhs.theSize = 0; 
     rhs.theCapacity = 0; 
    } 

    Vector & operator= (Vector && rhs) 
    { 
     std::swap(theSize, rhs.theSize); 
     std::swap(theCapacity, rhs.theCapacity); 
     std::swap(objects, rhs.objects); 

     return *this; 
    } 

    bool empty() const 
    { 
     return size() == 0; 
    } 
    int size() const 
    { 
     return theSize; 
    } 
    int capacity() const 
    { 
     return theCapacity; 
    } 

    Object & operator[](int index) 
    { 
     return objects[index]; 
    } 

    const Object & operator[](int index) const 
    { 
     return objects[index]; 
    } 

    void resize(int newSize) 
    { 
     if (newSize > theCapacity) 
      reserve(newSize * 2); 
     theSize = newSize; 
    } 

    void reserve(int newCapacity) 
    { 
     if (newCapacity < theSize) 
      return; 

     Object *newArray = new Object[newCapacity]; 
     for (int k = 0; k < theSize; ++k) 
      newArray[k] = std::move(objects[k]); 

     theCapacity = newCapacity; 
     std::swap(objects, newArray); 
     delete[] newArray; 
    } 

    // Stacky stuff 
    void push_back(const Object & x) 
    { 
     if (theSize == theCapacity) 
      reserve(2 * theCapacity + 1); 
     objects[theSize++] = x; 
    } 
    // Stacky stuff 
    void push_back(Object && x) 
    { 
     if (theSize == theCapacity) 
      reserve(2 * theCapacity + 1); 
     objects[theSize++] = std::move(x); 
    } 

    void pop_back() 
    { 
     --theSize; 
    } 

    const Object & back() const 
    { 
     return objects[theSize - 1]; 
    } 

    // Iterator stuff: not bounds checked 
    typedef Object * iterator; 
    typedef const Object * const_iterator; 

    iterator begin() 
    { 
     return &objects[0]; 
    } 
    const_iterator begin() const 
    { 
     return &objects[0]; 
    } 
    iterator end() 
    { 
     return &objects[size()]; 
    } 
    const_iterator end() const 
    { 
     return &objects[size()]; 
    } 

    static const int SPARE_CAPACITY = 2; 

    iterator insert(iterator & iter, const Object& obj){ 
     if (theSize >= theCapacity){ 
      resize(theSize+1); 
      int *p = iter; 
      for (iter; iter != this->end(); iter++){ 
       //cout << "test1" << endl; 
      } 
      for (iter; iter != p; iter--){ 
       *(iter-1) = *(iter-2); 
       cout << "test1" << endl; 
       //cout << *(iter - 2) << endl; 
       //cout << *(iter - 1) << endl; 
      } 
     } 
     else{ 
      int *p = iter; 
      for (iter; iter != this->end(); iter++){ 
       cout << "test" << endl; 
      } 
      for (iter; iter != p; iter--){ 
       *(iter-1) = (*iter-2); 
      } 

     } 
     *iter = obj; 
     cout << theSize << endl << theCapacity << endl; 
     //theSize++; 
     return this->begin(); 
    } 
    iterator erase(iterator iter){ 

    } 
    iterator find(iterator x, iterator y, const Object obj){ 

    } 
private: 
    int theSize; 
    int theCapacity; 
    Object * objects; 
}; 

#endif 

我的测试文件是这样的:

#include "Vector.h" 
#include <iostream> 
using namespace std; 

int main(){ 
    Vector<int> input; 
    Vector<int>::iterator iter; 
    int data = 0; 
    cout << "Enter five int digits: " << endl; 
    for (int i = 0; i < 5; i++){ 
     cin >> data; 
     input.push_back(data); 
    } 
    data = 7654; 
    iter = input.begin(); 
    iter++; 
    input.insert(iter, data); 

    for (iter = input.begin(); iter != input.end(); iter++){ 
     cout << *iter << endl; 
    } 

    system("PAUSE"); 

} 
+0

调用'resize'后,'iter'不再有效。你释放了它用来指向的内存。 –

+0

Iter是指向由'objects'指向的数组中的位置的指针。这个数组被'resize(theSize + 1)'替换和删除,所以现在指向释放内存,引用经典游戏“Game over!”。 – user4581301

+0

谢谢你们。有什么解决方案? –

回答

1

感谢user4581301和伊戈尔的评论,我能解决这个问题。调整数组大小时,必须先找到迭代器的索引,然后才能找到迭代器的索引。调整大小后,将迭代器设置为索引处对象的内存地址。像这样:

if (theSize >= theCapacity){ 
      int index = iter - this->begin(); 
      resize(theSize+1); 
      iter = &objects[index]; 
      int *p = iter;