File: foreach.yo

package info (click to toggle)
c%2B%2B-annotations 10.6.0-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 10,536 kB
  • ctags: 3,247
  • sloc: cpp: 19,157; makefile: 1,521; ansic: 165; sh: 128; perl: 90
file content (40 lines) | stat: -rw-r--r-- 2,090 bytes parent folder | download
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
    hi(for_each)
    itemization(
        it() Header file: tt(<algorithm>)
        it() Function prototype:
        itemization(
            itt(Function for_each(ForwardIterator first,
                ForwardIterator last, Function func);)
        )
        it() Description:
        itemization(
            it() Each of the elements implied by the iterator range
rangett(first, last) is passed in turn as a reference to the function (or
function object) tt(func). The function may modify the elements it receives
(as the used iterator is a forward iterator). Alternatively, if the elements
should be transformed, tt(transform) (see section ref(TRANSFORM)) can be
used. The function itself or a copy of the provided function object is
returned: see the example below, in which an extra argument list is added to
the tt(for_each) call, which argument is eventually also passed to the
function given to tt(for_each). Within tt(for_each) the return value of the
function that is passed to it is ignored. The tt(for_each) generic algorithm
looks a lot like the range-based for loop, but different from the range-based
for-loop the tt(for_each) algoritm can also be used with sub-ranges and with
reverse-iterators.
        )
        it() Example:
        verbinclude(-a examples/foreach.cc)
        it() Here is another example using a function object:
        verbinclude(-a examples/foreachclass.cc)
    )
    The example also shows that the tt(for_each) algorithm may be used with
functions defining tt(const) and non-tt(const) parameters. Also, see section
ref(TRANSFORM) for differences between the tt(for_each) and tt(transform)
generic algorithms.

    The tt(for_each) algorithm cannot directly be used (i.e., by passing
tt(*this) as the function object argument) inside a member function to modify
its own object as the tt(for_each) algorithm first creates its own copy of the
passed function object. A emi(lambda function) or a em(wrapper class) whose
constructor accepts a pointer or reference to the current object and possibly
to one of its member functions solves this problem.