COMP2004
Programming Practice
2002 Summer School


Kevin Pulo
School of Information Technologies
University of Sydney


(page 1)


String streams




(page 2)


String stream versions




(page 3)


Old string streams: strstream




(page 4)


istrstream example


#include
#include
#include
int main() {
string s = "here is a short string";
istrstream is(s.c_str());
string w;
while (is >> w) {
cout << w << endl;
}
}


(page 5)


ostrstream example


#include
#include
#include
#include
int main() {
char a[14] = "";
ostrstream os(a, 14);
os << setw(12) << setfill('*')
<< 42.12 << endl;
string s(a); cout << s;
}



(page 6)


New string streams: sstream


http://www.cs.usyd.edu.au/~kev/pp/sstream


(page 7)


istringstream example


#include
#include

int main() {
string s = "here is a short string";
istringstream is(s);
string w;
while (is >> w) {
cout << w << endl;
}
}


(page 8)


ostringstream example


#include
#include
#include

int main() {
ostringstream os;
os << setw(12) << setfill('*')
<< 42.12 << endl;
string s = os.str();
cout << s;
}


(page 9)


Iterators




(page 10)


Terminology






(page 11)


Basic Concepts




(page 12)


Basic Concepts




(page 13)


Input Iterator




(page 14)


Input Iterator II




(page 15)


Output Iterators




(page 16)


Iterator Example


template
OI copy(II begin, II end, OI out) {
for (; begin != end; ++out, ++begin)
*out = *begin;
return out;
}


(page 17)


Forward Iterator




(page 18)


Bidirectional Iterator


template
OI reverse_copy(BI begin, BI end,
OI out) {
while (begin != end)
*out++ = *--end;
return out;
}


(page 19)


Bidirectional Example


template
void reverse(BI begin, BI end) {
while ( (begin != end) &&
(begin != --end) ) {
swap(*begin++, *end);
}
}


(page 20)


Random Access Iterator




(page 21)


Random Access Example


template
void random_shuffle(RI begin, RI end) {
if (begin == end) return;
for (RI i = begin + 1; i != end; ++i) {
RI other = begin +
nrand(i - begin + 1);
swap(*i, *other);
}
}


(page 22)


Ranges




(page 23)


Containers




(page 24)


Containers




(page 25)


Container Typedefs




(page 26)


Container Typedefs




(page 27)


Container Members




(page 28)


Container Abstractions




(page 29)


Sequence Abstractions





(page 30)


Associative Abstractions






(page 31)


Associative Abstractions


































































































































































































(page 32)