如果对赋给指针的迭代器进行自增操作,该指针是否保留原始内存位置?

if you increment an iterator assigned to a pointer, will the pointer hold the original memory location?

本文关键字:指针 是否 保留 原始 位置 内存 操作 迭代器 如果      更新时间:2023-10-16

我的代码一直崩溃,我相信这是因为当我在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();
    }

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

整个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");
}

感谢user4581301和Igor的评论,我能够解决它。必须在数组大小调整时迭代器的索引丢失之前找到它。调整大小后,将迭代器设置为索引处对象的内存地址。这样的:

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