[英]How to write a random access custom iterator that can be used with STL algorithms?
#include <iostream>
#include <vector>
#include <algorithm>
template <typename T>
class _iterator
{
T* ptr;
public:
using iterator_category = std::random_access_iterator_tag;
using value_type = T;
using reference = T&;
using pointer = T*;
using difference_type = unsigned long long;
_iterator(T* ptr) : ptr(ptr) {}
reference operator* () { return *ptr; }
pointer operator->() { return ptr; }
_iterator& operator++() { ptr++; return *this; }
_iterator& operator--() { ptr--; return *this; }
difference_type operator-(const _iterator& it) { return this->ptr - it.ptr; }
_iterator operator+(const difference_type& diff) { return _iterator(ptr + diff); }
_iterator operator-(const difference_type& diff) { return _iterator(ptr - diff); }
bool operator==(const _iterator& it) { return this->ptr == it.ptr; }
bool operator!=(const _iterator& it) { return this->ptr != it.ptr; }
bool operator< (const _iterator& it) { return this->ptr < it.ptr; }
operator _iterator<const T>() const { return _iterator<const T>(ptr); }
};
template <typename T>
class X
{
std::vector<T> data;
public:
using iterator = _iterator<T>;
using const_iterator = _iterator<const T>;
void push_back(T t)
{
data.push_back(t);
}
iterator begin()
{
return iterator(&data[0]);
}
iterator end()
{
return iterator(&data[0] + data.size());
}
};
int main()
{
X<int> x;
for (int i = 9; i >= 0; i--)
x.push_back(i);
// 1 ------------------------------------------------------------------------
// C2780 'void std::_Sort_unchecked(_RanIt,_RanIt,iterator_traits<_Iter>::difference_type,_Pr)': expects 4 arguments - 3 provided
// C2672 '_Sort_unchecked': no matching overloaded function found
// C2678 binary '-': no operator found which takes a left - hand operand of type 'const _RanIt' (or there is no acceptable conversion)
std::sort(x.begin(), x.end());
// 2 ------------------------------------------------------------------------
// C2666 '_iterator<T>::operator -': 2 overloads have similar conversions
// C2512 '_iterator<T>': no appropriate default constructor available
for (X<int>::iterator it = x.end() - 1; it >= x.begin(); --it)
std::cout << *it << ' ';
std::cout << std::endl;
}
我正在嘗試為自定義容器編寫隨機訪問迭代器。 我面臨幾個問題:
錯誤寫在與其對應的每段代碼之上。 另外我不確定我定義iterator
和const_iterator
的方式是否有效。 我在這里做錯了什么以及如何解決它們?
// C2678 binary '-': no operator found which takes a left - hand operand of type 'const _RanIt' (or there is no acceptable conversion)
如果將以下 function 標記為const
:
difference_type operator-(const _iterator& it) const { return this->ptr - it.ptr; }
您將開始解決一些問題。 我沒有檢查這是否涵蓋了所有情況。 但總的來說,讓你能做的一切都const
有很大幫助。
我需要將函數和運算符標記為const
並為operator[]
編寫定義。 如果我想使用它們,我可以選擇提供關系運算符( >
、 >=
、 <=
)的重置。 這是工作代碼:
#include <iostream>
#include <vector>
#include <algorithm>
template <typename T>
class _iterator
{
T* ptr;
public:
using iterator_category = std::random_access_iterator_tag;
using value_type = T;
using reference = T&;
using pointer = T*;
using difference_type = unsigned long long;
_iterator(T* ptr) : ptr(ptr) {}
reference operator* () const { return *ptr; }
pointer operator->() const { return ptr; }
_iterator& operator++() { ptr++; return *this; }
_iterator& operator--() { ptr--; return *this; }
difference_type operator-(const _iterator& it) const { return this->ptr - it.ptr; }
_iterator operator+(const difference_type& diff) const { return _iterator(ptr + diff); }
_iterator operator-(const difference_type& diff) const { return _iterator(ptr - diff); }
reference operator[] (const difference_type& offset) const { return *(*this + offset); }
bool operator==(const _iterator& it) const { return this->ptr == it.ptr; }
bool operator!=(const _iterator& it) const { return this->ptr != it.ptr; }
bool operator< (const _iterator& it) const { return this->ptr < it.ptr; }
bool operator> (const _iterator& it) const { return this->ptr > it.ptr; }
bool operator>=(const _iterator& it) const { return !(this->ptr < it.ptr); }
bool operator<=(const _iterator& it) const { return !(this->ptr > it.ptr); }
operator _iterator<const T>() const { return _iterator<const T>(ptr); }
};
template <typename T>
class X
{
std::vector<T> data;
public:
using iterator = _iterator<T>;
using const_iterator = _iterator<const T>;
void push_back(T t)
{
data.push_back(t);
}
iterator begin()
{
return iterator(&data[0]);
}
iterator end()
{
return iterator(&data[0] + data.size());
}
};
int main()
{
X<int> x;
for (int i = 0; i < 10; i++)
x.push_back(i);
std::sort(x.begin(), x.end(), std::greater<>());
for (X<int>::iterator it = x.end() - 1; it >= x.begin(); --it)
std::cout << *it << ' ';
std::cout << std::endl;
}
Output:
0 1 2 3 4 5 6 7 8 9
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.