我试图实现一个与列表的STL实现几乎相同的模板类。
现在我正在研究std::insert()
函数的范围重载。原型是:
template <class InputIterator> iterator insert (const_iterator position, InputIterator first, InputIterator last);
问题是:我无法让first
和last
迭代器做任何事情。每当我试图取消引用它们时(*first
),我就会得到一个非法间接的&#34;编译时出错。我可以执行的唯一操作是first++
,当我使用std::vector<int>::iterator
进行尝试时,首先显然是返回下一个元素。
这是我班级中最重要的部分:
template <class T>
class List {
public:
typedef T value_type;
typedef value_type& reference;
typedef const value_type& const_reference;
typedef value_type* pointer;
typedef const value_type* const_pointer;
typedef std::size_t size_type;
/* Structures / Classes */
struct Node {
Node* previous;
Node* next;
Node() : previous(this), next(this) {};
Node(Node* previous) : previous(previous), next(this) {};
};
struct DataNode : public Node {
value_type data;
DataNode(const_reference data) : Node(), data(data) {};
virtual ~DataNode() {};
};
class ListIterator : public std::iterator<std::bidirectional_iterator_tag, value_type>
{
friend class List;
private:
Node* node;
public:
ListIterator() : node(0) {};
ListIterator(Node* ptr) : node(ptr) {};
ListIterator(const ListIterator& other) : node(other.node) {};
virtual ~ListIterator() {};
ListIterator& operator++() { node = node->next; return *this; };
ListIterator operator++(int) {
ListIterator tmp(*this);
operator++();
return tmp;
};
ListIterator& operator--() { node = node->previous; return *this; };
ListIterator operator--(int) {
ListIterator tmp(*this);
operator--();
return tmp;
};
bool operator==(const ListIterator& other) { return node == other.node; };
bool operator!=(const ListIterator& other) { return node != other.node; };
ListIterator& operator=(const ListIterator& other) {
if (this != &other) this->node = other.node;
return *this;
}
value_type& operator*() { return reinterpret_cast<DataNode*>(node)->data; }
};
class ConstListIterator : public std::iterator<std::bidirectional_iterator_tag, const value_type> {
/* same with const ... */
};
/* More typedefs... */
typedef ListIterator iterator;
typedef ConstListIterator const_iterator;
typedef std::reverse_iterator<iterator> reverse_iterator;
typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
typedef typename std::iterator_traits<iterator>::difference_type difference_type;
protected:
/* Class members */
Node* root; // at the end
size_type elementCount;
public:
/* functions... */
iterator insert(const_iterator position, const value_type& val);
iterator insert(const_iterator position, size_type n, const value_type& val);
template<class InputIterator>
iterator insert(const_iterator position, InputIterator first, InputIterator last);
iterator insert(const_iterator position, value_type&& val);
iterator insert(const_iterator position, std::initializer_list<value_type> il);
/* more functions... */
};
功能声明:
template <class T>
template <class InputIterator>
inline typename List<T>::iterator List<T>::insert(const_iterator position, InputIterator first, InputIterator last)
{
/* i don't know how to deal with the InputIterator */
value_type a(*first); // Illegal indirection
iterator it(first); // not working
}
欢迎任何帮助