gpt4 book ai didi

c++ - 如何保护我的迭代器,使其在指向 .end() 时抛出异常

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:10:14 24 4
gpt4 key购买 nike

我写了这个 Vector 类和一个嵌套的 Iterator 类和一个 const_Iterator 类,现在我必须保护我的 Vector 所以我的 Iterator 不会指向 .end() 之外,例如我的 operator++ 方法并抛出异常,如果我试试所以在我的 iterator 类中我无法访问 .end() 因为它是一个 vector 方法。所以我在想,我没有指向我的 Iterator 类中的值,而是指向整个 vector ,但我无法使用 Vector*< 访问 .end()/ 。我的解决方案是否正确?如果正确,我该如何传递我的 Vector,以便我可以按照我想要的方式使用它?

class Vector{
public:
using value_type= double;
using size_type= size_t;
using difference_type= ptrdiff_t;
using reference = double&;
using const_reference= const double&;
using pointer = double*;
using const_pointer= const double*;
using iterator = double*;
using const_iterator= const double*;
private:
size_t sz;
size_t max_sz;
double* values=nullptr;
class const_Iterator{
public:
using value_type = double;
using difference_type = ptrdiff_t;
using reference = double&;
using pointer = double*;
using iterator_category = std::forward_iterator_tag;
private:
double* ptr;
size_t cnt;
public:
const_Iterator(double* p){
ptr=p;
cnt=0;

}
const_Iterator& operator++ () {
ptr++;
cnt = 0;
return *this;
}
bool operator==(const const_Iterator& rop)const {
return this->ptr == rop.ptr;
}

bool operator!=(const const_Iterator& rop)const {
return this->ptr != rop.ptr;
}
const double operator* () const {
return *ptr;
}
friend Vector::difference_type operator-(const Vector::const_Iterator& lop,const Vector::const_Iterator& rop) {
return lop.ptr-rop.ptr;
}
};
class Iterator{
public:
using value_type = double;
using difference_type = ptrdiff_t;
using reference = double&;
using pointer = double*;
using iterator_category = std::forward_iterator_tag;
private:
double* ptr;
size_t cnt;
public:
Iterator(double* p){
ptr=p;
cnt=0;

}
Iterator& operator++() {
ptr++;
cnt = 0;
return *this;
}
Iterator operator++(int){
Iterator a(ptr);
ptr++;
return a;
}
bool operator==( Iterator& rop) {
return this->ptr != rop.ptr;
}

bool operator!=(const Iterator& rop) {
return this->ptr != rop.ptr;
}
double& operator*() {
return *ptr;
}
operator const_Iterator() const{

return const_Iterator(ptr);
};



};
const_Iterator end() const{return const_Iterator(values+sz);}
const_Iterator begin() const{return const_Iterator(values);}
Iterator begin() { return values; }
Iterator end() { return values + sz; }
size_t min_sz = 5;
Vector();
Vector(size_t);
Vector(const Vector&);
Vector (initializer_list<double> );
void push_back(double);
void reserve(size_t);
void pop_back();
bool empty();
void clear();
Vector& operator=(const Vector&);
const double& operator[] (size_t) const;
double& operator[] (size_t) ;

void fit_to_shrink();
size_t size()const {return sz;}
ostream& print(ostream&) const;
};

最佳答案

您的迭代器可能如下所示:

class Iterator{
public:
// ... using type

private:
Vector* parent;
std::size_t index;
public:
Iterator(Vector& v, std::size_t index) : ptr(&v), index(index) {}

Iterator& operator++() {
if (index == parent->size()) {
throw std::runtime_error("++ on end iterator");
}
++index;
return *this;
}

Iterator operator++(int){
Iterator old(*this);
++(*this);
return old;
}

bool operator==(const Iterator& rhs) const {
if (parent != rhs.parent) {
throw std::runtime_error("You compare iterator of different containers");
}
return index == rhs.index;
}

bool operator!=(const Iterator& rop) const { return !(*this == rhs); }

double& operator*() { return parent->at(index); } // `at` throws on invalid index

// ...

};

还有你的载体:

Iterator Vector::begin() { return Iterator(this, 0);}
Iterator Vector::end() { return Iterator(this, size());}

关于c++ - 如何保护我的迭代器,使其在指向 .end() 时抛出异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42911544/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com