用于`std :: sort`的自定义迭代器

时间:2019-05-01 18:20:28

标签: c++ c++17

我正在尝试创建自定义容器和迭代器。

这是我到目前为止所拥有的:

std::array<int, 1000> global_data;

class TestVectorIterator
{
public:
    TestVectorIterator()
        : index(0)
    {
    }

    TestVectorIterator(int index)
        : index(index)
    {
    }

    int& operator*()
    {
        return global_data[index];
    }

    const int& operator*() const
    {
        return global_data[index];
    }

    TestVectorIterator& operator++()
    {
        index++;
        return *this;
    }

    TestVectorIterator& operator--()
    {
        index--;
        return *this;
    }

    friend int operator- (const TestVectorIterator& lhs, const TestVectorIterator& rhs)
    {
        return lhs.index - rhs.index;
    }

    friend int operator+ (const TestVectorIterator& lhs, const TestVectorIterator& rhs)
    {
        return lhs.index + rhs.index;
    }

    friend bool operator== (const TestVectorIterator& lhs, const TestVectorIterator& rhs)
    {
        if (rhs.index >= global_data.size())
        {
            return lhs.index == rhs.index;
        }

        return (*lhs) == (*rhs);
    }

    friend bool operator!= (const TestVectorIterator& lhs, const TestVectorIterator& rhs)
    {
        if (rhs.index >= global_data.size())
        {
            return lhs.index != rhs.index;
        }

        return (*lhs) != (*rhs);
    }

    friend bool operator<= (const TestVectorIterator& lhs, const TestVectorIterator& rhs)
    {
        if (rhs.index >= global_data.size())
        {
            return lhs.index <= rhs.index;
        }

        return (*lhs) <= (*rhs);
    }

    friend bool operator>= (const TestVectorIterator& lhs, const TestVectorIterator& rhs)
    {
        if (rhs.index >= global_data.size())
        {
            return lhs.index >= rhs.index;
        }

        return (*lhs) >= (*rhs);
    }

    friend bool operator< (const TestVectorIterator& lhs, const TestVectorIterator& rhs)
    {
        if (rhs.index >= global_data.size())
        {
            return lhs.index < rhs.index;
        }

        return (*lhs) < (*rhs);
    }

    using difference_type = int;
    using value_type = int;
    using pointer = int*;
    using reference = int&;
    using iterator_category = std::random_access_iterator_tag;

private:
    int index = 0;
};

class TestVector
{
public:

    typedef TestVectorIterator iterator;
    typedef const TestVectorIterator const_iterator;

    TestVector()
    {
    }

    int size()
    {
        return global_data.size();
    }

    TestVector::iterator begin()
    {
        return TestVectorIterator(0);
    }

    TestVector::iterator end()
    {
        return TestVectorIterator(size());
    }

    TestVector::const_iterator cbegin()
    {
        return TestVectorIterator(0);
    }

    TestVector::const_iterator cend()
    {
        return TestVectorIterator(size());
    }

    int& operator[](int i)
    {
        return global_data[i];
    }
};

在范围for循环中工作正常,但与std::sort一起使用时会出现编译时错误:

enter image description here

我已经有一段时间没有使用C ++了,所以我猜想我缺少了一些简单的东西(或者做的只是简单的错误)。

编辑:

在回答Holt之后,这里是编译代码:

std::array<int, 1000> global_data;

class TestVectorIterator
{
public:
    TestVectorIterator()
        : index(0)
    {
    }

    TestVectorIterator(int index)
        : index(index)
    {
    }

    int& operator*()
    {
        return global_data[index];
    }

    const int& operator*() const
    {
        return global_data[index];
    }

    TestVectorIterator& operator++()
    {
        index++;
        return *this;
    }

    TestVectorIterator& operator--()
    {
        index--;
        return *this;
    }

    friend int operator- (const TestVectorIterator& lhs, const TestVectorIterator& rhs)
    {
        return lhs.index - rhs.index;
    }

    friend TestVectorIterator operator-(TestVectorIterator const& lhs, int rhs)
    {
        return TestVectorIterator(lhs.index - rhs);
    }

    friend TestVectorIterator operator+(TestVectorIterator const& lhs, int rhs)
    {
        return TestVectorIterator(lhs.index + rhs);
    }

    friend TestVectorIterator operator+(int lhs, TestVectorIterator const& rhs)
    {
        return TestVectorIterator(lhs + rhs.index);
    }

    friend TestVectorIterator& operator+= (TestVectorIterator& lhs, const TestVectorIterator& rhs)
    {
        lhs.index += rhs.index;
        return lhs;
    }

    friend TestVectorIterator& operator-= (TestVectorIterator& lhs, const TestVectorIterator& rhs)
    {
        lhs.index -= rhs.index;
        return lhs;
    }

    friend bool operator== (const TestVectorIterator& lhs, const TestVectorIterator& rhs)
    {
        if (rhs.index >= global_data.size())
        {
            return lhs.index == rhs.index;
        }

        return (*lhs) == (*rhs);
    }

    friend bool operator!= (const TestVectorIterator& lhs, const TestVectorIterator& rhs)
    {
        if (rhs.index >= global_data.size())
        {
            return lhs.index != rhs.index;
        }

        return (*lhs) != (*rhs);
    }

    friend bool operator<= (const TestVectorIterator& lhs, const TestVectorIterator& rhs)
    {
        if (rhs.index >= global_data.size())
        {
            return lhs.index <= rhs.index;
        }

        return (*lhs) <= (*rhs);
    }

    friend bool operator>= (const TestVectorIterator& lhs, const TestVectorIterator& rhs)
    {
        if (rhs.index >= global_data.size())
        {
            return lhs.index >= rhs.index;
        }

        return (*lhs) >= (*rhs);
    }

    friend bool operator< (const TestVectorIterator& lhs, const TestVectorIterator& rhs)
    {
        if (rhs.index >= global_data.size())
        {
            return lhs.index < rhs.index;
        }

        return (*lhs) < (*rhs);
    }

    using difference_type = int;
    using value_type = int;
    using pointer = int*;
    using reference = int&;
    using iterator_category = std::random_access_iterator_tag;

private:
    int index = 0;
};

1 个答案:

答案 0 :(得分:3)

要使用std::sort,您的迭代器必须满足LegacyRandomAccessIterator的要求,这意味着LegacyBidirectionalIteratorLegacyForwardIteratorLegacyIterator

您的TestVectorIterator正在满足一系列重载要求。特别是:

  • operator+(TestVectorIterator const&, TestVectorIterator const&)毫无意义,您需要operator+(TestVectorIterator const&, int)operator+(int, TestVectorIterator const&);
  • 您需要TestVectorIterator operator-(TestVectorIterator const&, int);
  • 您需要复合运算符:+=-=;
  • 您需要后缀递增和递减TestVectorIterator operator++(int)TestVectorIterator operator--(int)
  • 您需要一个下标运算符int& operator[](int)