美文网首页
2022-03-12【c++】自定義迭代器

2022-03-12【c++】自定義迭代器

作者: 持刀的要迟到了 | 来源:发表于2022-03-12 19:53 被阅读0次

百度直接搜到的那篇文章,代码无法正确运行。垃圾滚。

Writing a custom iterator in modern C++ - Internal Pointers

以下代码经过测试可知,编译通过必须要有的方法:四个重载运算符:*(指针) ++ == !=
主类中两个必须方法:begin end,而且需要返回迭代器的结构体。

#include <iostream> // For standard streams

#include <iterator> // For std::forward_iterator_tag
#include <cstddef>  // For std::ptrdiff_t


struct Iterator
{
    //using iterator_category = std::forward_iterator_tag;
    //using difference_type = std::ptrdiff_t;
    //using value_type = int;
    using pointer = int*;  // or also value_type*
    using reference = int&;  // or also value_type&

        // Iterator tags here...

    Iterator(pointer ptr) : m_ptr(ptr) {}

    reference operator*() const { return *m_ptr; }
    //pointer operator->() { return m_ptr; }

    // Prefix increment
    Iterator& operator++() { m_ptr++; return *this; }

    // Postfix increment
    //Iterator operator++(int) { Iterator tmp = *this; ++(*this); return tmp; }

    friend bool operator== (const Iterator& a, const Iterator& b) { return a.m_ptr == b.m_ptr; };
    friend bool operator!= (const Iterator& a, const Iterator& b) { return a.m_ptr != b.m_ptr; };
private:

    pointer m_ptr;
};


class Integers
{
public:

    // Iterator definition here ...

    Iterator begin() { return Iterator(&m_data[0]); }
    Iterator end() { return Iterator(&m_data[200]); } // 200 is out of bounds


    void SetData()
    {
        for (int i = 0; i < sizeof(this->m_data); i += sizeof(int))
        {
            int data = i / sizeof(int);
            m_data[data] = data;
        }
    }

private:
    int m_data[200];

};

int main()
{
    Integers integers;
    integers.SetData();

    for (auto i : integers)
        std::cout << i << "\n";
}
#include <iostream> // For standard streams

//#include <iterator> // For std::forward_iterator_tag
//#include <cstddef>  // For std::ptrdiff_t

struct Vec
{
public:

    int x = -99999;
    int y = -99999;

    Vec(int x, int y)
    {
        this->x = x;
        this->y = y;
    }

    void Set(int _x, int _y)
    {
        this->x = _x;
        this->y = _y;
    }

    void operator=(Vec& other)
    {
        this->x = other.x;
        this->y = other.y;
    }

    Vec operator+(const Vec& other) const
    {
        return Vec(this->x + other.x, this->y + other.y);
    }

    bool operator==(const Vec& other) const
    {
        if (this->x == other.x && this->y == other.y)
        {
            return true;
        }
        return false;
    }
};


struct FRect_Iterator
{
    friend class FRect;
    //using iterator_category = std::forward_iterator_tag;
    //using difference_type = std::ptrdiff_t;
    //using value_type = int;
    using pointer = Vec*;  // or also value_type*
    using reference = Vec&;  // or also value_type&

public:

    int x, y;

    FRect_Iterator(bool isStart)
    {
        if (isStart)
        {
            x = 2;
            y = 3;
        }
        else
        {
            x = 2 + 4;
            y = 3 + 5;
        }
        m_ptr = new Vec(x, y);
    }


    reference operator*() const { return *m_ptr; }
    //pointer operator->() { return m_ptr; }

    // Prefix increment
    FRect_Iterator& operator++() 
    { 
        x++;
        if (x >= 2 + 4) // x:0 right:50 get:0-49
        {
            x = 2;
            y++;
        }

        //When the value is one step more than the last, it's an end iterator
        if (y >= 3 + 5)
        {
            throw std::logic_error("Cannot increment an end iterator.");
        }

        m_ptr->Set(x, y);

        return *this; 
    }

    // Postfix increment
    //Iterator operator++(int) { Iterator tmp = *this; ++(*this); return tmp; }

    friend bool operator== (const FRect_Iterator& a, const FRect_Iterator& b) { return *a.m_ptr == *b.m_ptr; };
    friend bool operator!= (const FRect_Iterator& a, const FRect_Iterator& b) { return !(*a.m_ptr == *b.m_ptr); };
private:

    pointer m_ptr;
};


class FRect
{
public:

    FRect(int _x, int _y, int _w, int _h)
    {
        this->x = _x;
        this->y = _y;
        this->w = _w;
        this->h = _h;
    }

    // Iterator definition here ...

    FRect_Iterator begin() { return FRect_Iterator(true); }
    FRect_Iterator end() { return FRect_Iterator(false); }

private:
    int x, y, w, h;

};

int main()
{
    FRect rect(2, 3, 4, 5);
    //integers.SetData();

    for (auto i : rect)
    {
        std::cout << i.x << " " << i.y << "\n";
    }

}

相关文章

网友评论

      本文标题:2022-03-12【c++】自定義迭代器

      本文链接:https://www.haomeiwen.com/subject/ljlldrtx.html