具有 LinkedList 的迭代器类
Iterator class with a LinkedList?
我还是C++新手,我觉得这一定是我遗漏的非常明显的错误。
#include <iostream>
#include "List.h"
int main()
{
List<int> mylist;
List<int>::iterator it1, it2;
这条线
List<int>::iterator it1, it2;
给出消息"列表没有成员迭代器"
这是列表类
template <typename Object>
class List
{
private:
// The basic doubly linked List node.
// Nested inside of List, can be public
// because the Node is itself private
class Node
{
Object _data;
Node *_prev;
Node *_next;
Node(const Object & d = Object(), Node * p = NULL, Node * n = NULL)
: _data(d), _prev(p), _next(n) { }
};
public:
public:
List()
{
init();
}
~List()
{
clear();
delete _head;
delete _tail;
}
List(const List & rhs)
{
init();
*this = rhs;
}
const List & operator= (const List & rhs)
{
if (this == &rhs)
return *this;
clear();
for (const_iterator itr = rhs.begin(); itr != rhs.end(); ++itr)
push_back(*itr);
return *this;
}
// Return iterator representing beginning of List.
// Mutator version is first, then accessor version.
iterator begin()
{
return iterator(_head->_next);
}
const_iterator begin() const
{
return const_iterator(_head->_next);
}
// Return iterator representing endmarker of List.
// Mutator version is first, then accessor version.
iterator end()
{
return iterator(_tail);
}
const_iterator end() const
{
return const_iterator(_tail);
}
// Return number of elements currently in the List.
int size() const
{
return _size;
}
// Return true if the List is empty, false otherwise.
bool empty() const
{
return size() == 0;
}
void clear()
{
while (!empty())
pop_front();
}
// front, back, push_front, push_back, pop_front, and pop_back
// are the basic double-ended queue operations.
Object & front()
{
return *begin();
}
const Object & front() const
{
return *begin();
}
Object & back()
{
return *--end();
}
const Object & back() const
{
return *--end();
}
void push_front(const Object & x)
{
insert(begin(), x);
}
void push_back(const Object & x)
{
insert(end(), x);
}
void pop_front()
{
erase(begin());
}
void pop_back()
{
erase(--end());
}
// Insert x before itr.
iterator insert(iterator itr, const Object & x)
{
Node *p = itr.current;
_size++;
return iterator(p->_prev = p->_prev->_next = new Node(x, p->_prev, p));
}
// Erase item at itr.
iterator erase(iterator itr)
{
Node *p = itr.current;
iterator retVal(p->_next);
p->_prev->_next = p->_next;
p->_next->_prev = p->_prev;
delete p;
_size--;
return retVal;
}
iterator erase(iterator start, iterator end)
{
for (iterator itr = start; itr != end; )
itr = erase(itr);
return end;
}
private:
int _size;
Node *_head;
Node *_tail;
void init()
{
_size = 0;
_head = new Node;
_tail = new Node;
_head->_next = _tail;
_tail->_prev = _head;
}
};
我假设此错误可能是因为没有迭代器类?
我假设此错误可能是因为没有迭代器类?
是的,您的代码假定List<T>
还提供嵌套的List<T>::iterator
类。
这就是代码中缺少的内容。
相关文章:
- 使用std::multimap迭代器创建std::list
- 来自 std::list 的迭代器 .end() 按预期返回"0xcdcdcdcdcdcdcdcd"但 .begin()
- C++中带有List类的迭代器Segfault
- 如何在c++迭代器类型中包装std::chrono
- 集合上的输出迭代器:assign和increment迭代器
- Boost Spirit,获取迭代器内部语义动作
- 对于set上的循环-获取next元素迭代器
- 为什么output_editor Concept不需要output_e迭代器标记
- c++17文件系统::recursive_directory迭代器()在mac上没有给出这样的目录,但在windows上
- 使用迭代器时如何访问对象在向量中的位置?
- std::vector::迭代器是否可以合法地作为指针
- 跟随整数索引列表的自定义类迭代器
- 不明白迭代器,引用和指针失效,一个例子
- 我可以使用反向迭代器作为ForwardIt吗
- ESP8266单片机矢量迭代器的C++问题
- 如何在C++中将迭代器作为函数参数传递
- 是否应避免从非常量迭代器转换为常量迭代器?
- 如何在 c++ 中将字符串迭代器变量传递给函数?
- 为什么 vector 的随机访问迭代器给出与指针不同的内存地址?
- 具有 LinkedList 的迭代器类