我正在尝试创建一个自定义容器和迭代器。
这是我到目前为止的内容:
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
一起使用时会出现编译时错误:我已经有一段时间没有使用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;
};
最佳答案
为了使用 std::sort
,您的迭代器必须满足LegacyRandomAccessIterator的要求,这意味着LegacyBidirectionalIterator,LegacyForwardIterator和LegacyIterator。
您的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)
。 关于c++ - 在 `std::sort`中使用的自定义迭代器,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/55940777/