File: partition.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 (44 lines) | stat: -rw-r--r-- 1,259 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
    #include <algorithm>
    #include <iostream>
    #include <string>
    #include <iterator>
    using namespace std;

    void show(int *begin, int *end)
    {
        copy(begin, end, ostream_iterator<int>{ cout, " " });
        cout << '\n';
    }

    int main()
    {
        int org[] = { 1, 3, 5, 7, 9, 10, 2, 8, 6, 4 };
        int ia[10];

        copy(org, org + 10, ia);

        auto lessThan4 = [=](int value)
                        {
                            return value <= 4;
                        };

        int *split = partition(ia, ia + 10, lessThan4);

        cout << "Last ia[]- element <= 4 is ia[" << split - ia - 1 << "]\n";
        show(ia, end(ia));

        copy(org, org + 10, ia);
        split = stable_partition(ia, ia + 10, lessThan4);
        cout << "Last org[]-element <= 4 is ia[" << split - ia - 1 << "]\n";
        show(ia, end(ia));

        cout << "org[]-elements up to the partition point 4 are:\n";
        show(org, partition_point(org, org + 10, lessThan4));
    }
    //  Displays:
    //    Last ia[]- element <= 4 is ia[3]
    //    1 3 4 2 9 10 7 8 6 5
    //    Last org[]-element <= 4 is ia[3]
    //    1 3 2 4 5 7 9 10 8 6
    //    org[]-elements up to the partition point 4 are:
    //    1 3