如果对赋给指针的迭代器进行自增操作,该指针是否保留原始内存位置?
if you increment an iterator assigned to a pointer, will the pointer hold the original memory location?
我的代码一直崩溃,我相信这是因为当我在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;
相关文章:
- 新分配指向函数的指针是否合法?
- 在函数结束后使用指向变量的指针是否安全?
- C++ 返回指向函数内定义的静态数组的指针是否有效?
- 经过最后一个数组元素末尾的指针是否应该等于超过整个数组末尾的指针?
- 传递给放置 new 的指针是否是指向其对象表示形式的非 UB 指针?
- 'this'指针是否可以在 c++ 标头声明中使用?
- C++ 在向量中使用指针是否可能导致指针悬空?
- 唯一指针是否在堆或堆栈上分配内存?
- 将基本实例指针强制转换为派生实例指针是否合法?(实例不是派生实例)
- 当向量增长时,指向向量元素的C++指针是否无效
- 如何判断指针是否是指向数组的指针
- 在类方法中使用 "this" 指针是否是一种好的做法?
- 如何知道指针是否已在其他地方释放
- 结构成员指针是否自动初始化为零?
- 为静态类创建指向对象的指针是否合法?
- 新的自动关键字指针是否会自行删除?
- 创建指针是否超过非数组指针的末尾,而不是从 C++17 中的一元运算符和未定义的行为派生?
- 检查指针是否指向矢量元素
- 如何创建一个带锁的指针数组并检查指针是否空闲?
- 在映射中插入更多元素后,指向 QMap 中元素的指针是否仍然有效?