我正在尝试创建自定义容器和迭代器。
这是我到目前为止所拥有的:
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;
};
答案 0 :(得分:3)
要使用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)
。