1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
|
Generic algorithms often require a target container into which the results of
the algorithm are deposited. For example, the link(tt(copy))(COPY) generic
algorithm has three parameters. The first two define the range of visited
elements, the third defines the first position where the results
of the copy operation should be stored.
With the tt(copy) algorithm the number of elements to copy is usually
available beforehand, since that number can usually be provided by pointer
arithmetic. However, situations exist where pointer arithmetic cannot be
used. Analogously, the number of resulting elements sometimes differs from the
number of elements in the initial range. The generic algorithm
link(tt(unique_copy))(UNIQUECP) is a case in point. Here the number of
elements that are copied to the destination container is normally not known
beforehand.
In situations like these an emi(inserter) hi(adaptor: inserter) adaptor
function can often be used to create elements in the destination container.
There are three types of inserter adaptors:
itemization(
iti(back_inserter): calls the container's ti(push_back) member to add
new elements at the end of the container. E.g., to copy all elements of
tt(source) in reversed order to the back of tt(destination), using the
link(tt(copy))(COPY) generic algorithm:
verb(
copy(source.rbegin(), source.rend(), back_inserter(destination));
)
iti(front_inserter) calls the container's ti(push_front) member, adding
new elements at the beginning of the container. E.g., to copy all elements of
tt(source) to the front of the destination container (thereby also reversing
the order of the elements):
verb(
copy(source.begin(), source.end(), front_inserter(destination));
)
iti(inserter) calls the container's ti(insert) member adding new elements
starting at a specified starting point. E.g., to copy all elements of
tt(source) to the destination container, starting at the beginning of
tt(destination), shifting up existing elements to beyond the newly inserted
elements:
verb(
copy(source.begin(), source.end(), inserter(destination,
destination.begin()));
)
)
The inserter adaptors
hi(adaptor: required typedefs) hi(inserter: required typedefs)
require the existence of two tt(typedef)s:
itemization(
itt(typedef Data value_type), where tt(Data) is the data type stored in
the class offering tt(push_back, push_front) or tt(insert) members (Example:
tt(typedef std::string value_type));
itt(typedef value_type const &const_reference)
)
Concentrating on tt(back_inserter), this iterator expects the name of a
container supporting a member tt(push_back). The inserter's tt(operator())
member calls the container's tt(push_back) member. Objects
hi(inserter: and non-STL classes) of any class supporting a tt(push_back)
member can be passed as arguments to tt(back_inserter) provided the class adds
verb(
typedef DataType const &const_reference;
)
to its interface (where tt(DataType const &) is the type of the parameter
of the class's member tt(push_back)). Example:
verbinclude(-a examples/backinserter.cc)
|