我的代码一直在崩溃,我相信它是因为当我在Vector类的insert函数中向后循环时,我将迭代器递减超过原始指针变量。这是插入功能:
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");
}
答案 0 :(得分:1)
感谢user4581301和Igor的评论,我能够解决它。在调整数组大小时,必须先找到迭代器的索引。调整大小后,将迭代器设置为索引处对象的内存地址。像这样:
if (theSize >= theCapacity){
int index = iter - this->begin();
resize(theSize+1);
iter = &objects[index];
int *p = iter;