gpt4 book ai didi

c++ - 随机访问迭代器 : What am I missing?

转载 作者:搜寻专家 更新时间:2023-10-31 00:50:11 25 4
gpt4 key购买 nike

我有一个简单的基于数组的容器,我想为它制作一个迭代器。我的目标是让 std::sort 正常工作,所以我使用随机访问迭代器。

迭代器类:

class MyIterator: public std::iterator<std::random_access_iterator_tag, T> {
friend class ArraySequence;
private:
T* pos;
MyIterator(T* pos);
public:
MyIterator(const MyIterator &it);
~MyIterator();
public:
typename MyIterator::reference operator*() const;
typename MyIterator::pointer operator->() const;
typename MyIterator::reference operator[](const typename MyIterator::difference_type& n) const;
MyIterator operator++(int);
MyIterator& operator++();
MyIterator operator--(int);
MyIterator& operator--();
MyIterator operator+(const typename MyIterator::difference_type& n) const;
MyIterator& operator+=(const typename MyIterator::difference_type& n);
MyIterator operator-(const typename MyIterator::difference_type& n) const;
MyIterator& operator-=(const typename MyIterator::difference_type& n);
bool operator!=(const MyIterator& it) const;
bool operator==(const MyIterator& it) const;
bool operator<(const MyIterator& it) const;
bool operator>(const MyIterator& it) const;
bool operator<=(const MyIterator& it) const;
bool operator>=(const MyIterator& it) const;
};

它的方法:

template <typename T>
ArraySequence<T>::MyIterator::MyIterator(T* pos): pos(pos) {}

template <typename T>
ArraySequence<T>::MyIterator::MyIterator(const MyIterator& it): pos(it.pos) {}

template <typename T>
typename ArraySequence<T>::MyIterator::reference ArraySequence<T>::MyIterator::operator*() const {
return *pos;
}

template <typename T>
typename ArraySequence<T>::MyIterator::pointer ArraySequence<T>::MyIterator::operator->() const {
return pos;
}

template <typename T>
typename ArraySequence<T>::MyIterator::reference ArraySequence<T>::MyIterator::operator[](const typename MyIterator::difference_type& n) const {
return *(pos + n);
}

template <typename T>
typename ArraySequence<T>::MyIterator ArraySequence<T>::MyIterator::MyIterator::operator++(int) {
return MyIterator(pos++);
}

template <typename T>
typename ArraySequence<T>::MyIterator& ArraySequence<T>::MyIterator::MyIterator::operator++() {
++pos;
return *this;
}

template <typename T>
typename ArraySequence<T>::MyIterator ArraySequence<T>::MyIterator::MyIterator::operator--(int) {
return MyIterator(pos--);
}

template <typename T>
typename ArraySequence<T>::MyIterator& ArraySequence<T>::MyIterator::MyIterator::operator--() {
--pos;
return *this;
}

template <typename T>
typename ArraySequence<T>::MyIterator ArraySequence<T>::MyIterator::operator+(const typename MyIterator::difference_type& n) const {
return MyIterator(pos + n);
}

template <typename T>
typename ArraySequence<T>::MyIterator& ArraySequence<T>::MyIterator::operator+=(const typename MyIterator::difference_type& n) {
pos += n;
return *this;
}

template <typename T>
typename ArraySequence<T>::MyIterator& ArraySequence<T>::MyIterator::operator-=(const typename MyIterator::difference_type& n) {
pos -= n;
return *this;
}

template <typename T>
typename ArraySequence<T>::MyIterator ArraySequence<T>::MyIterator::operator-(const typename MyIterator::difference_type& n) const {
return MyIterator(pos - n);
}

template <typename T>
bool ArraySequence<T>::MyIterator::operator!=(const MyIterator& it) const {
return pos != it.pos;
}

template <typename T>
bool ArraySequence<T>::MyIterator::operator==(const MyIterator& it) const {
return pos == it.pos;
}

template <typename T>
bool ArraySequence<T>::MyIterator::operator<(const MyIterator& it) const {
return pos < it.pos;
}

template <typename T>
bool ArraySequence<T>::MyIterator::operator>(const MyIterator& it) const {
return pos > it.pos;
}

template <typename T>
bool ArraySequence<T>::MyIterator::operator<=(const MyIterator& it) const {
return pos <= it.pos;
}

template <typename T>
bool ArraySequence<T>::MyIterator::operator>=(const MyIterator& it) const {
return pos >= it.pos;
}

template <typename T>
ArraySequence<T>::MyIterator::~MyIterator() {}

当我尝试运行 std::sort 时,我收到很多编译器错误,例如“difference_type __len = __last - __第一的;”。我在这里错过了什么?有什么可以改进的?

最佳答案

您忘记了您需要为迭代器本身实现operator -。随机访问迭代器支持 it1 - it2 来获取两个迭代器之间的距离。您有 operator - 用于 difference_type,但没有用于迭代器类型本身。您需要添加一个重载,例如:

template <typename T>
typename MyIterator::difference_type ArraySequence<T>::MyIterator::operator-(const typename ArraySequence<T>::MyIterator& n) const
{
return pos - n.pos;
}

关于c++ - 随机访问迭代器 : What am I missing?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58276770/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com