[英]Automatically create iterator from class that implements operator[]
Suppose I have a class that implements operator[]
, eg: 假设我有一个实现operator[]
的类,例如:
class Array {
public:
Array(size_t s) : data(new int[s]) {}
~Array() { delete[] data; }
int& operator[](size_t index) {
return data[index];
}
private:
int* data;
};
Is there a way to create a random access iterator from the class without having to manually create the iterator class and all its methods? 有没有一种方法可以从类中创建随机访问迭代器,而不必手动创建迭代器类及其所有方法? I could manually define the class as follows: 我可以手动定义该类,如下所示:
class ArrayIterator : public std::iterator<std::random_access_iterator_tag, int> {
public:
ArrayIterator(Array& a) : arr(a), index(0) {}
reference operator[](difference_type i) {
return arr[index + i];
}
ArrayIterator& operator+=(difference_type i) {
index += i;
return *this;
}
bool operator==(const ArrayIterator& rhs) {
return &arr == &rhs.arr && index == rhs.index;
}
// More methods here...
private:
Array& arr;
difference_type index;
};
But doing so is time consuming since there are so many methods to implement, and each iterator for a class with operator[] would have the exact same logic. 但是,这样做非常耗时,因为要实现的方法太多,而且带有operator []的类的每个迭代器都具有完全相同的逻辑。 It seems it would be possible for the compiler to do this automatically, so is there a way to avoid implementing the entire iterator? 似乎编译器可以自动执行此操作,因此有没有办法避免实现整个迭代器?
Is there a way to create a random access iterator from the class without having to manually create the iterator class and all its methods? 有没有一种方法可以从类中创建随机访问迭代器,而不必手动创建迭代器类及其所有方法?
The simplest way to create a random-access iterator is to just use a raw pointer, which satisfies all of the requirements of the RandomAccessIterator
concept (the STL even provides a default template specialization of std::iterator_traits
for raw pointers), eg: 创建随机访问迭代器的最简单方法是仅使用原始指针,该指针可以满足RandomAccessIterator
概念的所有要求(STL甚至为原始指针提供了默认模板专业化std::iterator_traits
),例如:
class Array {
public:
Array(size_t s) : data(new int[s]), dataSize(s) {}
~Array() { delete[] data; }
int& operator[](size_t index) {
return data[index];
}
size_t size() const { return dataSize; }
int* begin() { return data; }
int* end() { return data+dataSize; }
const int* cbegin() const { return data; }
const int* cend() const { return data+dataSize; }
private:
int* data;
size_t dataSize;
};
Implementing the random access operator by using operator[]
may work, but it can be very inefficient and that's why compilers dont do that automatically. 通过使用operator[]
实现随机访问运算operator[]
可能会起作用,但是效率可能非常低,这就是编译器不自动执行此操作的原因。 Just imagine adding operator[]
to a class like std::list
, where "going to element i" may take up to i steps. 想象一下将operator[]
添加到std::list
类的类std::list
,其中“转到元素i”可能要花i步。 Incrementing an iterator based on operator[]
would then have complexity O(n), where n is the size of the list. 然后,根据operator[]
递增迭代器将具有复杂度O(n),其中n是列表的大小。 However, users of random access iterators, expect a certain efficiency, typically O(1). 但是,随机访问迭代器的用户期望一定的效率,通常为O(1)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.