rhughes
rhughes

Reputation: 9583

Custom iterator for use in `std::sort`

I am trying to create a custom container and iterator.

Here is what I have so far:

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

It is working fine with range for loops, but gives compile time error when used with std::sort:

enter image description here

I haven't used C++ in a while, so I'm guessing I'm missing something simple (or doing something just plain wrong).

Edit:

After taking Holt's answer, here is compiling code:

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

Upvotes: 4

Views: 1814

Answers (1)

Holt
Holt

Reputation: 37626

In order to use std::sort, your iterators must meet the requirements of LegacyRandomAccessIterator, which implies LegacyBidirectionalIterator, LegacyForwardIterator and LegacyIterator.

Your TestVectorIterator is lacking a bunch of overloads to meet such requirements. In particular:

  • operator+(TestVectorIterator const&, TestVectorIterator const&) makes no sense, you need operator+(TestVectorIterator const&, int) and operator+(int, TestVectorIterator const&);
  • you need TestVectorIterator operator-(TestVectorIterator const&, int);
  • you need compound operators: +=, -=;
  • you need postfix increment and decrement TestVectorIterator operator++(int) and TestVectorIterator operator--(int);
  • you need a subscript operator int& operator[](int).

Upvotes: 6

Related Questions