Thinking in C++ Vol 2 - Practical Programming |
Prev |
Home |
Next |
An iterator is an abstraction for genericity.
It works with different types of containers without knowing the underlying
structure of those containers. Most containers support iterators, so you can
say:
<ContainerType>::iterator
<ContainerType>::const_iterator
to produce the iterator types for a container. Every
container has a begin( ) member function that produces an iterator
indicating the beginning of the elements in the container, and an end( )
member function that produces an iterator which is the past-the-end marker of the container. If the container is const begin( )
and end( ) produce const iterators, which disallow changing
the elements pointed to (because the appropriate operators are const).
All iterators can advance within their sequence (via operator++)
and allow == and != comparisons. Thus, to move an iterator it
forward without running it off the end, you say something like:
while(it != pastEnd) {
// Do something
++it;
}
where pastEnd is the past-the-end marker produced by
the container s end( ) member function.
An iterator can be used to produce the container element
that it is currently selecting via the dereferencing operator (operator*).
This can take two forms. If it is an iterator traversing a container, and
f( ) is a member function of the type of objects held in the
container, you can say either:
or
Knowing this, you can create a template that works with any
container. Here, the apply( ) function template calls a member
function for every object in the container, using a pointer to member that is
passed as an argument:
//: C07:Apply.cpp
// Using simple iteration.
#include <iostream>
#include <vector>
#include <iterator>
using namespace std;
template<class Cont, class PtrMemFun>
void apply(Cont& c, PtrMemFun f) {
typename Cont::iterator it = c.begin();
while(it != c.end()) {
((*it).*f)(); // Alternate form
++it;
}
}
class Z {
int i;
public:
Z(int ii) : i(ii) {}
void g() { ++i; }
friend ostream& operator<<(ostream& os,
const Z& z) {
return os << z.i;
}
};
int main() {
ostream_iterator<Z> out(cout, " ");
vector<Z> vz;
for(int i = 0; i < 10; i++)
vz.push_back(Z(i));
copy(vz.begin(), vz.end(), out);
cout << endl;
apply(vz, &Z::g);
copy(vz.begin(), vz.end(), out);
} ///:~
You can t use operator-> here because the
resulting statement would be:
which attempts to use the iterator s operator->*,
which is not provided by the iterator classes.
It is much easier to use either for_each( ) or transform( )
to apply functions to sequences, as you saw in the previous chapter.
Thinking in C++ Vol 2 - Practical Programming |
Prev |
Home |
Next |