简体   繁体   English

创建一个简单的前向迭代器,它自动在循环缓冲区的“末端”换行

[英]Create a simple forward iterator which automatically wraps at the “end” of a circular buffer

I've created a simple circular buffer by inheriting std::vector and overloading its operator[] to modulo the desired index with the vector size:我通过继承 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
}

which correctly produces the output:正确生成 output:

0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9

I would also like to create a "smart" forward input iterator which supports operator++ (increment) and operator* (dereference) and which automatically "wraps" when advancing past the last element in the vector back to the beginning of the underlying vector to which it's associated.我还想创建一个“智能”前向输入迭代器,它支持 operator++ (增量)和 operator* (取消引用),并且在经过向量中的最后一个元素回到底层向量的开头自动“换行”它是相关的。

This hypothetical iterator would allow, for example, easier execution of some functions in the algorithm library (which often take iterators as arguments) on a subset of the circvector without adding logic to check for and then split into two separate calls when the desired subset spans the end->start wrap of the circvector.例如,这个假设的迭代器将允许在循环向量的子集上更轻松地执行算法库中的某些函数(通常将迭代器作为参数),而无需添加逻辑来检查,然后在所需的子集跨越时拆分为两个单独的调用圆向量的结束->开始环绕。

I've been reading up on creating custom iterators but descriptions get pretty hairy with information that seemingly satisfies requirements I don't think I need.我一直在阅读有关创建自定义迭代器的内容,但描述变得非常复杂,其中包含看似满足我认为不需要的要求的信息。 Add to that discussion of recent deprecation of std::iterator and I'm not sure where to even begin to create my own iterator, or associate it with my circvector class.再加上最近弃用 std::iterator 的讨论,我什至不知道从哪里开始创建我自己的迭代器,或者将它与我的 circvector class 相关联。

Can someone get me started with a minimally workable template to run with?有人可以让我开始使用一个最低限度可行的模板来运行吗?

This class appears to achieve the desired behavior, though it inherits from the deprecated std::iterator 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
    };
};

It was modified from https://lorenzotoso.wordpress.com/2016/01/13/defining-a-custom-iterator-in-c/它是从https://lorenzotoso.wordpress.com/2016/01/13/defining-a-custom-iterator-in-c/修改而来

A test program is:一个测试程序是:

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] << " ";
}

creating output:创建 output:

0 1 2 3 4 5 6 7 8 9
42 42 42 3 4 5 6 7 42 42

The use of algorithms from begin() to end() of course no longer works, since begin()==end().从 begin() 到 end() 的算法的使用当然不再有效,因为 begin()==end()。 But you can operate on partial segments of the buffer using algorithms (like std::for_each as shown) as long as the length is 1 less than the full buffer size.但是您可以使用算法(如所示的 std::for_each )对缓冲区的部分段进行操作,只要长度比完整缓冲区大小小 1 即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM