File: upperbound2.cc

package info (click to toggle)
c%2B%2B-annotations 12.2.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 13,044 kB
  • sloc: cpp: 24,337; makefile: 1,517; ansic: 165; sh: 121; perl: 90
file content (93 lines) | stat: -rw-r--r-- 3,172 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#include <algorithm>
#include <iostream>
using namespace std;

int main()
{
    using pic =  pair<int, char>;

    pic picArr[] =
        { {1, 'f'}, {5, 'r'}, {5, 'a'}, {7, 'n'}, {8, 'k'} };
    pic *picArrEnd = picArr + size(picArr);

    cout << "Sequence: ";
    for (auto &pair: picArr)
        cout << '{' << pair.first << ',' << pair.second << "}, ";
    cout << '\n';

    auto iter = lower_bound(picArr, picArrEnd, 5,
                    [&](pic const &range, int value)
                    {
                        return range.first < value;
                    }
                );
    cout << "  lower bound, <,  {5,?} can be inserted before {" <<
                iter->first << ',' << iter->second << "}\n";

    iter = upper_bound(picArr, picArrEnd, 5,
                    [&](int value, pic const &range)
                    {
                        return value < range.first;
                    }
                );
    cout << "  upper_bound, <,  {5,?} can be inserted before {" <<
                iter->first << ',' << iter->second << "}\n";

    iter = upper_bound(picArr, picArrEnd, 9,
                    [&](int value, pic const &range)
                    {
                        return value < range.first;
                    }
                );
    cout << "  upper_bound, <,  {9,?} can be inserted " <<
                ( &*iter == picArrEnd ? "at the end" : "???") << '\n';

    sort(picArr, picArrEnd,
        [](pic const &lhs, pic const &rhs)
        {
            return lhs.first > rhs.first;
        }
    );

    cout << "\nSequence: ";
    for (auto &pair: picArr)
        cout << '{' << pair.first << ',' << pair.second << "}, ";
    cout << '\n';

    iter = lower_bound(picArr, picArrEnd, 5,
                    [&](pic const &range, int value)
                    {
                        return range.first > value;
                    }
                );
    cout << "  lower_bound, >,  {5,?} can be inserted before {" <<
                iter->first << ',' << iter->second << "}\n";

    iter = upper_bound(picArr, picArrEnd, 5,
                    [&](int value, pic const &range)
                    {
                        return value > range.first;
                    }
                );
    cout << "  upper_bound, >,  {5,?} can be inserted before {" <<
                iter->first << ',' << iter->second << "}\n";

    iter = upper_bound(picArr, picArrEnd, 0,
                    [&](int value, pic const &range)
                    {
                        return value > range.first;
                    }
                );
    cout << "  upper_bound, >,  {0,?} can be inserted " <<
                ( &*iter == picArrEnd ? "at the end" : "???") << '\n';
}
// Displays:
// Sequence: {1,f}, {5,r}, {5,a}, {7,n}, {8,k},
//   lower bound, <,  {5,?} can be inserted before {5,r}
//   upper_bound, <,  {5,?} can be inserted before {7,n}
//   upper_bound, <,  {9,?} can be inserted at the end
//
// Sequence: {8,k}, {7,n}, {5,r}, {5,a}, {1,f},
//   lower_bound, >,  {5,?} can be inserted before {5,r}
//   upper_bound, >,  {5,?} can be inserted before {1,f}
//   upper_bound, >,  {0,?} can be inserted at the end