[英]Create a simple forward iterator which automatically wraps at the “end” of a circular buffer
我通过继承 std::vector 并重载其 operator[] 创建了一个简单的循环缓冲区,以将所需的索引与向量大小取模:
template <typename T>
class circvector : public std::vector<T> {
public:
T& operator[](size_t index) { return *(this->data() + index%this->size()); }; // modulo index by vector size when accessing with [] operator
};
int main()
{
circvector<int> buffer; // create a circvector
buffer.resize(10); // resize it
std::iota(buffer.begin(), buffer.end(), 0); // fill with monotonically increasing integers
for (int i = 0; i < buffer.size() * 2; i++)
std::cout << buffer[i] << " "; // access elements with [] beyond the circvector size is safe
}
正确生成 output:
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9
我还想创建一个“智能”前向输入迭代器,它支持 operator++ (增量)和 operator* (取消引用),并且在经过向量中的最后一个元素回到底层向量的开头时自动“换行”它是相关的。
例如,这个假设的迭代器将允许在循环向量的子集上更轻松地执行算法库中的某些函数(通常将迭代器作为参数),而无需添加逻辑来检查,然后在所需的子集跨越时拆分为两个单独的调用圆向量的结束->开始环绕。
我一直在阅读有关创建自定义迭代器的内容,但描述变得非常复杂,其中包含看似满足我认为不需要的要求的信息。 再加上最近弃用 std::iterator 的讨论,我什至不知道从哪里开始创建我自己的迭代器,或者将它与我的 circvector class 相关联。
有人可以让我开始使用一个最低限度可行的模板来运行吗?
这个 class 似乎实现了所需的行为,尽管它继承自已弃用的 std::iterator class:
template <typename T>
class circvector: public std::vector<T> {
public:
T& operator[](size_t index_) { return *(this->data() + index_%this->size()); }; // modulo index_ by vector size when accessing with [] operator
class iterator; // forward declaration
iterator begin() { return circvector<T>::iterator(*this, 0); }
iterator end() { return circvector<T>::iterator(*this, this->size()); } // will be same as begin() due to modulo in iterator constructor initializer list
private:
class iterator : public std::iterator<std::output_iterator_tag, T> {
private:
circvector<T>& container_; // NOTE: ORDER MATTERS! // dependency injection
size_t index_{ 0 }; // NOTE: ORDER MATTERS! // state of iterator
public:
T& operator*() const { return container_[index_]; } // this uses the overloaded operator[] which includes modulo
iterator& operator+(int N) { index_ = (index_ + N) % container_.size(); return *this; } // random increment by N
iterator& operator++() { index_ = (index_ + 1) % container_.size(); return *this; } // increment with modulo
iterator operator++(int) { return ++(*this); } // just calls prefix increment: operator++()
bool operator!=(const iterator & right) const { return index_ != right.index_ % container_.size(); }
bool operator==(const iterator & right) const { return index_ == right.index_ % container_.size(); }
explicit iterator(circvector<T>& container, size_t index_ = 0) : container_(container), index_(index_ % container_.size()) {} // constructor
};
};
它是从https://lorenzotoso.wordpress.com/2016/01/13/defining-a-custom-iterator-in-c/修改而来
一个测试程序是:
int main()
{
circvector<int> buffer;
buffer.assign({ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 });
auto start_offset{ 8 };
auto end_offset{ start_offset + 5 };
for (int i = 0; i < buffer.size(); i++) std::cout << buffer[i] << " ";
std::cout << "\n";
std::for_each(buffer.begin() + start_offset, buffer.begin() + end_offset, [](auto& i) { i = 42; });
for (int i = 0; i < buffer.size(); i++) std::cout << buffer[i] << " ";
}
创建 output:
0 1 2 3 4 5 6 7 8 9
42 42 42 3 4 5 6 7 42 42
从 begin() 到 end() 的算法的使用当然不再有效,因为 begin()==end()。 但是您可以使用算法(如所示的 std::for_each )对缓冲区的部分段进行操作,只要长度比完整缓冲区大小小 1 即可。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.