c++ 为不返回true基础类型的迭代器实现operator->

oyt4ldly  于 2023-04-08  发布在  其他
关注(0)|答案(1)|浏览(80)

假设我想为一个表示2D点数组的类实现迭代器。迭代器应该使该类看起来像存储双精度对数组,但是这个类实际上使用了两个独立的数组(一个用于X坐标,一个用于Y坐标)。我想知道我应该如何实现迭代器的operator->()。我当然不能从operator*中获取返回值的地址,因为它是一个临时值。每次调用operator->()时,我应该在堆上分配一个pair<double&, double&>吗?有没有好的方法来做到这一点?

struct PointArray {
    std::vector<double> m_x;
    std::vector<double> m_y;

    // Define a custom iterator class for ControlPointArray
    class iterator {
    public:
        using value_type = std::pair<double&, double&> const;
        using reference = value_type;
        using pointer = value_type*;
        using difference_type = std::ptrdiff_t;
        using iterator_category = std::random_access_iterator_tag;

        iterator() = default;
        iterator(const typename std::vector<double>::iterator& x_it, const typename std::vector<double>::iterator& y_it)
            : m_x_it(x_it), m_y_it(y_it) {}

        reference operator*() const { return { *m_x_it, *m_y_it }; }
        pointer operator->() const {
            // how to implement this method ??
        }

        iterator& operator++() { ++m_x_it; ++m_y_it; return *this; }
        iterator operator++(int) { auto copy = *this; ++(*this); return copy; }
        iterator& operator--() { --m_x_it; --m_y_it; return *this; }
        iterator operator--(int) { auto copy = *this; --(*this); return copy; }
        iterator& operator+=(difference_type n) { m_x_it += n; m_y_it += n; return *this; }
        iterator operator+(difference_type n) const { auto copy = *this; copy += n; return copy; }
        iterator& operator-=(difference_type n) { return (*this) += -n; }
        iterator operator-(difference_type n) const { auto copy = *this; copy -= n; return copy; }
        difference_type operator-(const iterator& other) const { return m_x_it - other.m_x_it; }
        reference operator[](difference_type n) const { return *(*this + n); }
        bool operator==(const iterator& other) const { return m_x_it == other.m_x_it; }
        bool operator!=(const iterator& other) const { return !(*this == other); }
        bool operator<(const iterator& other) const { return m_x_it < other.m_x_it; }
        bool operator<=(const iterator& other) const { return m_x_it <= other.m_x_it; }
        bool operator>(const iterator& other) const { return m_x_it > other.m_x_it; }
        bool operator>=(const iterator& other) const { return m_x_it >= other.m_x_it; }

    private:
        typename std::vector<double>::iterator m_x_it;
        typename std::vector<double>::iterator m_y_it;
    };

    // Define begin() and end() functions to return iterators
    iterator begin() { return { m_x.begin(), m_y.begin() }; }
    iterator end() { return { m_x.end(), m_y.end() }; }

// the rest of the PointArray class...
};
nx7onnlm

nx7onnlm1#

返回一个本身包含值的伪指针。

struct pseudo_pointer {
   value pair_of_data;
   pointer operator->() const {return &pair_of_data;}
};

pseudo_pointer operator->() const {
    return pseudo_pointer{{ *m_x_it, *m_y_it }};
}

这是我的头上的伪代码,可能需要进一步调整编译

相关问题