13.01.2015 Views

Pensar en C++ (Volumen 1) - Grupo ARCO

Pensar en C++ (Volumen 1) - Grupo ARCO

Pensar en C++ (Volumen 1) - Grupo ARCO

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

✐<br />

✐<br />

✐<br />

“Volum<strong>en</strong>1” — 2012/1/12 — 13:52 — page 511 — #549<br />

✐<br />

16.7. Introducción a los iteradores<br />

iterator(PStash& pStash)<br />

: ps(pStash), index(0) {}<br />

// To create the <strong>en</strong>d s<strong>en</strong>tinel:<br />

iterator(PStash& pStash, bool)<br />

: ps(pStash), index(ps.next) {}<br />

// Copy-constructor:<br />

iterator(const iterator& rv)<br />

: ps(rv.ps), index(rv.index) {}<br />

iterator& operator=(const iterator& rv) {<br />

ps = rv.ps;<br />

index = rv.index;<br />

return *this;<br />

}<br />

iterator& operator++() {<br />

require(++index = 0,<br />

"PStash::iterator::operator-- "<br />

"moves index out of bounds");<br />

return *this;<br />

}<br />

iterator& operator--(int) {<br />

return operator--();<br />

}<br />

// Jump interator forward or backward:<br />

iterator& operator+=(int amount) {<br />

require(index + amount < ps.next &&<br />

index + amount >= 0,<br />

"PStash::iterator::operator+= "<br />

"attempt to index out of bounds");<br />

index += amount;<br />

return *this;<br />

}<br />

iterator& operator-=(int amount) {<br />

require(index - amount < ps.next &&<br />

index - amount >= 0,<br />

"PStash::iterator::operator-= "<br />

"attempt to index out of bounds");<br />

index -= amount;<br />

return *this;<br />

}<br />

// Create a new iterator that’s moved forward<br />

iterator operator+(int amount) const {<br />

iterator ret(*this);<br />

ret += amount; // op+= does bounds check<br />

return ret;<br />

}<br />

T* curr<strong>en</strong>t() const {<br />

return ps.storage[index];<br />

}<br />

T* operator*() const { return curr<strong>en</strong>t(); }<br />

511<br />

✐<br />

✐<br />

✐<br />

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!