File: sort.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 (65 lines) | stat: -rw-r--r-- 2,112 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
    // compile as: g++ -O2 sort.cc -ltbb
    #include <algorithm>
    #include <cstdlib>
    #include <execution>
    #include <functional>
    #include <iostream>
    #include <iterator>
    #include <string>
    using namespace std;

    int main()
    {
        string words[] =
                {
                    "november", "kilo", "mike", "lima",
                    "oscar", "quebec", "papa"
                };

        sort(words, words + 7);
        copy(words, words + 7, ostream_iterator<string>(cout, " "));
        cout << '\n';

        sort(words, words + 7, greater<string>());
        copy(words, words + 7, ostream_iterator<string>(cout, " "));
        cout << '\n';

        int *vect = new int[10'000];
        generate(execution::par, vect, vect + 10'000, random);
        cout << "sorted: " << is_sorted(vect, vect + 10'000) << '\n';

        sort(execution::par, vect, vect + 10'000);
        cout << "sorted: " << is_sorted(vect, vect + 10'000) << '\n';

        delete[] vect;

        // stable-sorting:

        using Pair = pair<size_t, string>;  // days & nrs of the months
        vector<Pair> months =
        {
            { 31, "Jan." }, { 28, "Feb." }, { 31, "Mar." }, { 30, "Apr." },
            { 31, "May." }, { 30, "Jun." }, { 31, "Jul." }, { 31, "Aug." },
            { 30, "Sep." }, { 31, "Oct." }, { 30, "Nov." }, { 31, "Dec." },
        };

        stable_sort(months.begin(), months.end(),
                    [&](Pair const &lhs, Pair const &rhs)
                    {
                        return lhs.first > rhs.first;
                    }
        );

        for (size_t idx = 0; auto const &month: months)
            cout << month.first << ": " << month.second <<
                (++idx % 4 == 0 ? "\n" : "  ");

    }
    //  Displays:
    //      kilo lima mike november oscar papa quebec
    //      quebec papa oscar november mike lima kilo
    //      unordered sequence
    //      sorted sequence
    //      31: Jan.  31: Mar.  31: May.  31: Jul.
    //      31: Aug.  31: Oct.  31: Dec.  30: Apr.
    //      30: Jun.  30: Sep.  30: Nov.  28: Feb.