C++ 堆栈上的前向迭代器
我必须在基于数组的堆栈上实现前向迭代器。我不能使用std::vectors或任何东西,我只需要它。当我开始使用前向迭代器,特别是运算符时,我停止了这个程序的开发 我有一个方法,它接受一个通用序列,然后根据该序列,给定一个偏移量,创建一个堆栈:C++ 堆栈上的前向迭代器,c++,iterator,C++,Iterator,我必须在基于数组的堆栈上实现前向迭代器。我不能使用std::vectors或任何东西,我只需要它。当我开始使用前向迭代器,特别是运算符时,我停止了这个程序的开发 我有一个方法,它接受一个通用序列,然后根据该序列,给定一个偏移量,创建一个堆栈: template <typename IterT> stack(IterT begin, IterT end) : _stack(0), _size(0), _capacity(0) { try {
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;
}
}
如果您想创建一个看起来像指针的迭代器,您不需要
索引
,因为数据
起着它的作用。比较运算符应比较数据
s,而不是值:
bool operator==(const const_iterator &other) const {
return data == other.data;
}
如果您想创建一个,它稍微复杂一点。首先,
operator++
应该减少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]);
}
请试着去掉不相关的部分,使其代码更少,并添加缺少的内容,以便我们可以复制它(另请参阅)。顺便问一下,堆栈构造函数中的如何清除以“销毁堆栈”?请注意,迭代器类不会用于堆栈sint(a,a+5)代码>。IterT
类型将被推断为int*
。我还建议您。顺便说一下,您的运算符==
不正确。当两个不同的迭代器指向不同的位置时,当那个位置的元素是相同的时,迭代器通常是如何工作的:<代码>运算符>()<>代码>,虽然没有使用,但是应该返回<代码>数据< /代码>本身,而不是它的地址,您不需要在迭代器中使用索引。您认为其他运算符正确吗?我可能想尝试第一个选项,然后如果时间足够,我将尝试切换到反向迭代器@Evg@man_o_war,操作员=代码>也应该更正。而操作符->()
应该返回数据本身。其余的看起来不错。如果使用运算符+++
递增数据
实现直接迭代器,则开始
中的数据
应指向结束
迭代器中的数据
之前。1)析构函数没有任何用处。2) 运算符->()
应返回数据。3) 整个private
部分是冗余的。4) begin()
应使用stack[0]
,end()
-使用stack[size]
(请记住,[begin,end]是的,谢谢,但是当我试图编译它的时候,我试着在我的开始和结束方法中将int
转换成const int*
,我不能忍受我该怎么做使用&\u stack[0]
和&\u stack[\u size]
。伙计,我真的很爱你
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;
}
}
bool operator==(const const_iterator &other) const {
return data == other.data;
}
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]);
}