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 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
|
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
// cout << "compare " << vectEl.val << ", " <<
// vectEl.ch << " to " <<
// value.val << ", " << value.ch << '\n';
int main()
{
struct X
{
int val;
char ch;
};
// lambda expression:
// 1st parameter: fixed value,
// 2nd parameter: element from the vector.
//
// sorted in ascending order: use 2nd-param. < 1st-param or
// 2nd-param <= 2nd-param
//
// If the value to sort with (= the key) is not present in the
// sorted vector then all variants (upper_bound, lower_bound,
// using > or >= when descendingly sorted, using < or <= when
// ascendingly sorted return the iterator to the positing where
// the key should be inserted.
//
// If the value to sort with (= the key) already is present in the
// sorted vector then:
// * lower_bound with <= and upper_bound with < return the
// iterator to the first occurrence of the key in the sorted
// vector;
// * lower_bound with < and upper_bound with <= return the
// iterator beyond the last occurrence of the key in the sorted
// vector
//
// sorted in descending order: use 2nd-param. > 1st-param or
// 2nd-param >= 2nd-param
//
// If the value to sort with (= the key) already is present in the
// sorted vector then:
// * lower_bound with >= and upper_bound with > return the
// iterator to the first occurrence of the key in the sorted
// vector;
// * lower_bound with > and upper_bound with >= return the
// iterator beyond the last occurrence of the key in the sorted
// vector
{
vector<X> values
{
{ 4, 'X' },
{ 1, 'a' },
{ 2, 'a' },
{ 3, 'a' },
{ 4, 'b' },
{ 4, 'c' },
{ 5, 'a' },
{ 7, 'a' },
{ 9, 'a' }
};
// UPDATE THE ABOVE TEXT ACCORDING TO THIS:
// note that the 1st param. refers to the vector, the 2nd to the value
// auto insert = upper_bound(
auto insert = lower_bound(
values.begin() + 1, values.end(),
values[0],
[&](X const &vectEl, X const &value)
{
return vectEl.val <= value.val;
// upper_bound: with <= at the beginning, lower_bound with <
// upper_bound: with < at the end, lower_bound with <=
}
);
cout << "FIRST Insert at " << (insert - values.begin()) << '\n';
// insert points at the 1st element <= value: the topmost
// element should be moved there.
rotate(values.begin(), values.begin() + 1, insert);
for (X const &value: values)
cout << value.val << ' ' << value.ch << ", ";
// upper_bound: 1 a, 2 a, 3 a, 4 X, 4 b, 4 c, 5 a, 7 a, 9 a,
// or lower-bound with <=
// lower bound: 1 a, 2 a, 3 a, 4 b, 4 c, 4 X, 5 a, 7 a, 9 a,
// or upper_bound with <=
cout << '\n';
}
{
vector<X> values
{
{ 4, 'X' },
{ 9, 'a' },
{ 7, 'a' },
{ 5, 'a' },
{ 4, 'b' },
{ 4, 'c' },
{ 3, 'a' },
{ 2, 'a' },
{ 1, 'a' }
};
// auto insert = upper_bound(
auto insert = lower_bound(
values.begin() + 1, values.end(),
values[0],
[&](X const &vectEl, X const &value)
{
return vectEl.val >= value.val;
}
);
// upper_bound: with >= at the beginning, lower_bound with >
// upper_bound: with > at the end, lower_bound with >=
cout << "FIRST: Insert at " << (insert - values.begin()) << '\n';
// insert points at the 1st element <= value: the topmost
// element should be moved there.
rotate(values.begin(), values.begin() + 1, insert);
for (X const &value: values)
cout << value.val << ' ' << value.ch << ", ";
// upper_bound: 9 a, 7 a, 5 a, 4 X, 4 b, 4 c, 3 a, 2 a, 1 a,
// or lower_bound with >=
// lower bound: 9 a, 7 a, 5 a, 4 b, 4 c, 4 X, 3 a, 2 a, 1 a,
// or upper_bound with >=
cout << '\n';
}
return 0;
{
vector<X> values
{
{ 4, 'X' },
{ 9, 'a' },
{ 7, 'a' },
{ 5, 'a' },
{ 3, 'a' },
{ 2, 'a' },
{ 1, 'a' }
};
auto insert = lower_bound(
values.rbegin(), values.rend() - 1,
values[0],
[&](X const &value, X const &vectEl)
{
return vectEl.val >= value.val;
}
);
cout << "Insert at " << (insert.base() - values.begin()) << '\n';
// insert points at the 1st element <= value: the topmost
// element should be moved there.
rotate(values.begin(), values.begin() + 1, insert.base());
for (X const &value: values)
cout << value.val << ' ' << value.ch << ", ";
// all variants (upper_bound, lower_bound, >, >= :
// 9 a, 7 a, 5 a, 4 X, 3 a, 2 a, 1 a,
cout << '\n';
}
}
|