File: findend.cc

package info (click to toggle)
c%2B%2B-annotations 13.02.02-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 13,576 kB
  • sloc: cpp: 25,297; makefile: 1,523; ansic: 165; sh: 126; perl: 90; fortran: 27
file content (48 lines) | stat: -rw-r--r-- 1,354 bytes parent folder | download | duplicates (2)
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
    #include <algorithm>
    #include <string>
    #include <iterator>
    #include <iostream>
    using namespace std;

    bool twice(size_t first, size_t second)
    {
        return first == (second << 1);
    }

    int main()
    {
        string sarr[] =
            {
                "alpha", "bravo", "charley", "delta", "echo",
                "foxtrot", "golf", "hotel",
                "foxtrot", "golf", "hotel",
                "india", "juliet", "kilo"
            };
        string search[] =
            {
                "foxtrot",
                "golf",
                "hotel"
            };
        auto past = end(sarr);

        copy
        (
            find_end(sarr, past, search, search + 3),   // sequence starting
            past, ostream_iterator<string>{ cout, " " } // at 2nd 'foxtrot'
        );
        cout << '\n';

        size_t range[] = { 2, 4, 6, 8, 10, 4, 6, 8, 10 };
        size_t nrs[]   = { 2, 3, 4 };

        copy                // sequence of values starting at last sequence
        (                   // of range[] that are twice the values in nrs[]
            find_end(range, range + 9, nrs, nrs + 3, twice),
            range + 9, ostream_iterator<size_t>{ cout, " " }
        );
        cout << '\n';
    }
    //  Displays:
    //      foxtrot golf hotel india juliet kilo
    //      4 6 8 10