stdx::TransformItr Class Template Reference
template<class I, class R, class C>
class stdx::TransformItr< I, R, C >
When working with STL containers of objects it is common to want to look at only one member in a class. For example, if we have a person class which contains names and occupations we might want to only print each object's name or we might want to find a specific object based on a occupation. To do this with straight STL we need to write our own little function objects. A transform iterator can do the same thing by wrapping a standard iterator such that a member function gets called on every dereference. For example, we could make a container of person objects look like a container of names. The code below uses a transform iterator to print out all the names of person objects and then finds the first person with the name "Jim Smith".
list<Person> lst; // ... fill lst with Person objects ... // Copy each person's name to standard out copy( trans_iseq(lst,&Person::getName), ostream_iterator<string>(cout,"\n") ); // Find first person with name "Jim Smith" lst<Person>::iteartor itr; itr = find( trans_iseq(lst,&Person::getName), "Jim Smith" ).base();
trans_iseq() is a helper function which creates an input sequence of two transform iterators from the given container and member function pointer. Notice the need for the call to the transform iterator's base() member function. This is because otherwise the find algorithm would return a transform iterator, but what we really want is a basic lst<Person>::iterator - or in other words the iterator which the transform iterator wraps.
Normal function pointers and function objects are not allowed as arguments to the trasform iterator constructor.
Public Types
Public Methods
- TransformItr ()
- TransformItr (I baseItr, R(C::*p)())
- operator * () const
- operator-> () const
- operator[] (difference_type n) const
- operator+ (difference_type n) const
- operator+= (difference_type n)
- operator- (difference_type n) const
- operator-= (difference_type n)
- operator++ (int)
- operator++ ()
- operator-- (int)
- operator-- ()
- base () const