我想对我的代码发表意见。我需要一个自定义容器的随机访问迭代器。 如果我阅读C ++ 2011标准,特别是第24章,我理解这是一个实现 可能是以下几点:
class my_iterator : public std::iterator<std::random_access_iterator_tag, my_element>
{
private:
my_container *_ref;
my_iterator_state state; // some useful state
public:
my_iterator ();
my_iterator (const my_iterator &);
my_iterator (my_iterator &&);
~my_iterator ();
my_iterator &operator = (const my_iterator &);
my_iterator &operator = (my_iterator &&);
reference operator * ();
my_iterator &operator ++ ();
bool operator != (my_iterator);
value_type operator * ();
pointer operator -> ();
my_iterator &operator * ();
void operator ++ (int);
value_type operator ++ (int);
const my_iterator &operator ++ (int);
reference operator ++ (int);
my_iterator &operator -- ();
const my_iterator operator -- (int);
reference operator -- (int);
my_iterator &operator += (difference_type);
my_iterator operator + (difference_type);
my_iterator operator - (difference_type);
reference operator [] (difference_type);
bool operator < (my_iterator);
bool operator > (my_iterator);
bool operator <= (my_iterator);
bool operator >= (my_iterator);
friend difference_type (my_iterator, my_iterator);
friend my_iterator operator + (difference_type, my_iterator);
friend void swap (iterator &, iterator &);
};
void swap (iterator &, iterator &);
difference_type (my_iterator, my_iterator);
my_iterator operator + (difference_type, my_iterator);
以上实施是否正确且功能齐全?还有成员?
答案 0 :(得分:3)
通常,您不能使用相同的参数但返回类型不同的重载,因此您的几个成员不能存在。你应该保留以下内容:
reference operator * ();
my_iterator operator ++ (int);
my_iterator operator -- (int);
并删除虚假超载:
value_type operator * ();
void operator ++ (int);
value_type operator ++ (int);
const my_iterator &operator ++ (int);
reference operator ++ (int);
const my_iterator operator -- (int);
reference operator -- (int);
这没有任何意义:
friend difference_type (my_iterator, my_iterator);
我认为应该是:
friend difference_type operator-(my_iterator, my_iterator);
你似乎错过了operator-=