Pensar en C++ (Volumen 1) - Grupo ARCO
Pensar en C++ (Volumen 1) - Grupo ARCO
Pensar en C++ (Volumen 1) - Grupo ARCO
You also want an ePaper? Increase the reach of your titles
YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.
✐<br />
✐<br />
✐<br />
“Volum<strong>en</strong>1” — 2012/1/12 — 13:52 — page 503 — #541<br />
✐<br />
16.7. Introducción a los iteradores<br />
}<br />
int pop() {<br />
require(top > 0, "Too many pop()s");<br />
return stack[--top];<br />
}<br />
class iterator;<br />
fri<strong>en</strong>d class iterator;<br />
class iterator {<br />
IntStack& s;<br />
int index;<br />
public:<br />
iterator(IntStack& is) : s(is), index(0) {}<br />
// To create the "<strong>en</strong>d s<strong>en</strong>tinel" iterator:<br />
iterator(IntStack& is, bool)<br />
: s(is), index(s.top) {}<br />
int curr<strong>en</strong>t() const { return s.stack[index]; }<br />
int operator++() { // Prefix<br />
require(index < s.top,<br />
"iterator moved out of range");<br />
return s.stack[++index];<br />
}<br />
int operator++(int) { // Postfix<br />
require(index < s.top,<br />
"iterator moved out of range");<br />
return s.stack[index++];<br />
}<br />
// Jump an iterator forward<br />
iterator& operator+=(int amount) {<br />
require(index + amount < s.top,<br />
"IntStack::iterator::operator+=() "<br />
"tried to move out of bounds");<br />
index += amount;<br />
return *this;<br />
}<br />
// To see if you’re at the <strong>en</strong>d:<br />
bool operator==(const iterator& rv) const {<br />
return index == rv.index;<br />
}<br />
bool operator!=(const iterator& rv) const {<br />
return index != rv.index;<br />
}<br />
fri<strong>en</strong>d ostream&<br />
operator