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

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

arco.esi.uclm.es
from arco.esi.uclm.es More from this publisher
13.01.2015 Views

✐ ✐ ✐ “Volumen1” — 2012/1/12 — 13:52 — page 510 — #548 ✐ Capítulo 16. Introducción a las Plantillas while(getline(file, line)) textlines.push(new string(line)); int i = 0; // Use iterator to print lines from the list: Stack::iterator it = textlines.begin(); Stack::iterator* it2 = 0; while(it != textlines.end()) { cout c_str()

✐ ✐ ✐ “Volumen1” — 2012/1/12 — 13:52 — page 511 — #549 ✐ 16.7. Introducción a los iteradores iterator(PStash& pStash) : ps(pStash), index(0) {} // To create the end sentinel: iterator(PStash& pStash, bool) : ps(pStash), index(ps.next) {} // Copy-constructor: iterator(const iterator& rv) : ps(rv.ps), index(rv.index) {} iterator& operator=(const iterator& rv) { ps = rv.ps; index = rv.index; return *this; } iterator& operator++() { require(++index = 0, "PStash::iterator::operator-- " "moves index out of bounds"); return *this; } iterator& operator--(int) { return operator--(); } // Jump interator forward or backward: iterator& operator+=(int amount) { require(index + amount < ps.next && index + amount >= 0, "PStash::iterator::operator+= " "attempt to index out of bounds"); index += amount; return *this; } iterator& operator-=(int amount) { require(index - amount < ps.next && index - amount >= 0, "PStash::iterator::operator-= " "attempt to index out of bounds"); index -= amount; return *this; } // Create a new iterator that’s moved forward iterator operator+(int amount) const { iterator ret(*this); ret += amount; // op+= does bounds check return ret; } T* current() const { return ps.storage[index]; } T* operator*() const { return current(); } 511 ✐ ✐ ✐ ✐

✐<br />

✐<br />

✐<br />

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

✐<br />

Capítulo 16. Introducción a las Plantillas<br />

while(getline(file, line))<br />

textlines.push(new string(line));<br />

int i = 0;<br />

// Use iterator to print lines from the list:<br />

Stack::iterator it = textlines.begin();<br />

Stack::iterator* it2 = 0;<br />

while(it != textlines.<strong>en</strong>d()) {<br />

cout c_str()

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

Saved successfully!

Ooh no, something went wrong!