堆栈上的前向迭代器
Forward Iterator on a Stack
我必须在基于数组的堆栈上实现前向迭代器。我不能使用 std::vectors 或任何东西,我只需要它。当我开始使用前向迭代器时,特别是使用运算符时,我对该程序的开发就停止了。
我有一个采用通用序列的方法,并从中给定偏移量,创建一个堆栈:
template <typename IterT>
stack(IterT begin, IterT end) : _stack(0), _size(0), _capacity(0) {
try {
for(; begin!=end; ++begin) {
push(static_cast<T>(*begin));
}
}
catch(...) {
clear(); //my method to destroy the stack
throw;
}
}
在我的主要情况下,我执行以下操作:
int a[5] = {1, 2, 3, 4, 5};
stack<int> sint(a, a+5);
cout << sint << endl;
但是当代码运行时,堆栈会创建但不打印。有人可以帮助我吗?并且还给我其他帮助(关于代码缩进、改进等......谢谢,我将转发迭代器代码。
class const_iterator {
const T* data;
unsigned int index;
public:
typedef std::forward_iterator_tag iterator_category;
typedef T value_type;
typedef ptrdiff_t difference_type;
typedef const T* pointer;
typedef const T& reference;
const_iterator() : data(0){}
const_iterator(const T* arr) : data(arr) {}
const_iterator(const const_iterator &other)
: data(other.data){ }
const_iterator& operator=(const const_iterator &other) {
data = other.data;
return *this;
}
~const_iterator() {
data = 0;
}
reference operator*() const {
return *data;
}
pointer operator->() const {
return &(data);
}
const_iterator operator++(int) {
const_iterator tmp(*this);
++*this;
return tmp;
}
const_iterator& operator++() {
++data;
return *this;
}
bool operator==(const const_iterator &other) const {
return data[index] == other.data[index];
}
bool operator!=(const const_iterator &other) const {
return data[index] != other.data[index] ;
}
private:
friend class stack;
const_iterator(unsigned int ind) :
index(ind){}
}; // class const_iterator
const_iterator begin() const {
cout << "begin" << _stack[_size-1] << endl;
return const_iterator(_stack[_size-1]);
}
const_iterator end() const {
cout << "end" << _stack[0] << endl;
return const_iterator(_stack[0]);
}
最后但并非最不重要的一点是,我重新定义了 <<运算符以适合迭代器:
template <typename T>
std::ostream &operator<<(std::ostream &os, const stack<T> &st) {
typename stack<T>::const_iterator i, ie;
for(i = st.begin(), ie = st.end(); i!=ie; ++i){
os << *i << std::endl;
}
return os;
}
堆栈的代码如下(为了可读性,我省略了一些东西(。
stack()
: _capacity(0), _size(0), _stack(0){}
void push (const T &value){
if (_size == _capacity){ //raddoppio la dimensione
if(_capacity == 0)
++_capacity;
_capacity *= 2;
T* tmp = new T[_capacity];
copy_n(_stack, _size, tmp);
swap(_stack, tmp);
delete[] tmp;
}
_stack[_size] = value;
++_size;
}
void pop(){
T _tmp;
if(!is_empty()){
_tmp = _stack[_size-1];
--_size;
}
}
如果你想创建一个看起来像指针的迭代器,你不需要index
,因为它data
发挥它的作用。比较运算符应比较data
s,而不是值:
bool operator==(const const_iterator &other) const {
return data == other.data;
}
如果要创建反向迭代器,则稍微复杂一些。首先,operator++
应该减少data
。其次,取消引用运算符不应返回*data
,而是返回*(data - 1)
。第三,begin()
迭代器中的data
应指向stack[size]
,end()
迭代器中的data
应指向stack[0]
。在任何情况下都不需要析构函数。
我遵循了之前的建议,这是编辑后的结果,但我仍然无法弄清楚如何在私有部分中正确使用构造函数
class const_iterator {
const T *data;
public:
/* ITERATOR TRAITS HERE */
const_iterator() : data(0){}
const_iterator(const T* arr) : data(arr) {}
const_iterator(const const_iterator &other)
: data(other.data){ }
const_iterator& operator=(const const_iterator &other) {
data = other.data;
return *this;
}
~const_iterator() {
data = 0;
}
reference operator*() const {
return *data;
}
pointer operator->() const {
return &(data);
}
const_iterator operator++(int) {
const_iterator tmp(*this);
++*this;
return tmp;
}
const_iterator& operator++() {
++data;
return *this;
}
bool operator==(const const_iterator &other) const {
return data == other.data;
}
bool operator!=(const const_iterator &other) const {
return data != other.data;
}
private:
friend class stack;
const_iterator(const T *d) {
data = d;
}
}; // classe const_iterator
const_iterator begin() const {
return const_iterator(_stack[_size-1]);
}
const_iterator end() const {
return const_iterator(_stack[0]);
}
相关文章:
- 使用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::迭代器是否可以合法地作为指针
- 我可以在没有堆栈的情况下在二叉搜索树中实现迭代器吗?
- 为什么某些 STL 容器(堆栈、队列、优先级队列)不支持迭代器?
- 堆栈上的前向迭代器
- 启用 MSVC 调试迭代器时堆栈分配器访问冲突
- 有效的迭代器范围从堆栈上的char
- C++中的堆栈迭代器
- 如何在 C++ 中为堆栈集声明迭代器
- 使用模板化堆栈编写迭代器
- 迭代器不是可取消引用的堆栈(表达式树)和循环无法正常工作