Skip to content

Commit eb1b019

Browse files
committed
cpp: make algorithm actually work, make pod awesomer
1 parent dcb2df4 commit eb1b019

4 files changed

Lines changed: 241 additions & 251 deletions

File tree

‎cpp/aggregate.cpp‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
#include "common.hpp"
22

33
int main() {
4-
// Aggregate initialization is a form of initializer list constructor.
4+
// # Aggregate initialization
5+
//
6+
// Aggregate is a form of initializer list constructor.
7+
//
58
// That constructor gets defined automatically.
9+
//
610
// It is only available for aggregate types.
11+
//
712
// Having such a constructor seems to be the main property of aggregates.
813
// https://en.cppreference.com/w/cpp/language/aggregate_initialization
914
{

‎cpp/algorithm.cpp‎

Lines changed: 91 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
#include "common.hpp"
22

33
int main() {
4+
// # min
5+
//
6+
// # max
47
{
58
assert(std::min(0.1, 0.2) == 0.1);
69
assert(std::max(0.1, 0.2) == 0.2);
@@ -21,21 +24,20 @@ int main() {
2124
assert((v == std::vector<int>{1, 0, 2}));
2225
}
2326

24-
/* # swap
25-
*
26-
* Does things equivalent to:
27-
*
28-
* template <class T> void swap (T& a, T& b)
29-
* {
30-
* T c(a); a=b; b=c;
31-
* }
32-
*
33-
* However stdlib can specialize it to do operations more efficiently.
34-
*
35-
* Some stdlib classes implement swap as a method.
36-
*
37-
* Particularly important because of the copy and swap idiom.
38-
*/
27+
// # swap
28+
//
29+
// Does things equivalent to:
30+
//
31+
// template <class T> void swap (T& a, T& b)
32+
// {
33+
// T c(a); a=b; b=c;
34+
// }
35+
//
36+
// However stdlib can specialize it to do operations more efficiently.
37+
//
38+
// Some stdlib classes implement swap as a method.
39+
//
40+
// Particularly important because of the copy and swap idiom.
3941

4042
// # random_shuffle
4143
{
@@ -44,18 +46,17 @@ int main() {
4446
}
4547

4648
#if __cplusplus >= 201703L
47-
/* # sample
48-
*
49-
* https://stackoverflow.com/questions/6942273/how-to-get-a-random-element-from-a-c-container/42484107#42484107
50-
*/
49+
// # sample
50+
//
51+
// https://stackoverflow.com/questions/6942273/how-to-get-a-random-element-from-a-c-container/42484107#42484107
5152
{
5253
const std::vector<int> in{1, 2, 3, 5, 7};
5354
std::vector<int> out;
5455
size_t nelems = 3;
5556
std::sample(in.begin(), in.end(), std::back_inserter(out),
5657
nelems, std::mt19937{std::random_device{}()});
57-
std::set in_set(std::begin(in), std::end(in));
58-
std::set out_set(std::begin(out), std::end(out));
58+
std::set<int> in_set{std::begin(in), std::end(in)};
59+
std::set<int> out_set{std::begin(out), std::end(out)};
5960

6061
// The sample elements are distinct.
6162
assert(out_set.size() == nelems);
@@ -73,41 +74,38 @@ int main() {
7374
}
7475
#endif
7576

76-
/* # equal
77-
*
78-
* Compares ranges of two containers.
79-
*
80-
* Vs memcmp:
81-
* https://stackoverflow.com/questions/39262496/why-is-stdequal-much-slower-than-a-hand-rolled-loop-for-two-small-stdarray
82-
*/
77+
// # equal
78+
//
79+
// Compares ranges of two containers.
80+
//
81+
// Vs memcmp:
82+
// https://stackoverflow.com/questions/39262496/why-is-stdequal-much-slower-than-a-hand-rolled-loop-for-two-small-stdarray
8383
{
8484
std::vector<int> v1{0, 1, 2 };
8585
std::vector<int> v2{ 1, 2, 3};
8686
assert(std::equal(v1.begin() + 1, v1.end(), v2.begin()));
8787
}
8888

89-
/* # copy
90-
*
91-
* Vs memcpy: can compile down to an assembly optimized memcpy for arrays apparently, so just always use this:
92-
* https://stackoverflow.com/questions/4707012/is-it-better-to-use-stdmemcpy-or-stdcopy-in-terms-to-performance
93-
*/
89+
// # copy
90+
//
91+
// Vs memcpy: can compile down to an assembly optimized memcpy for arrays apparently, so just always use this:
92+
// https://stackoverflow.com/questions/4707012/is-it-better-to-use-stdmemcpy-or-stdcopy-in-terms-to-performance
9493
{
95-
/* Newbie basic class usage. */
94+
// Newbie basic class usage.
9695
{
9796
std::vector<int> v{0, 1, 2, 3, 4};
9897
std::vector<int> v2(v.size(), v.size());
9998
std::copy(v.begin() + 1, v.end() - 1, v2.begin() + 2);
10099
assert(v2 == std::vector<int>({5, 5, 1, 2, 3}));
101100
}
102101

103-
/* # std::begin
104-
*
105-
* # std::end
106-
*
107-
* Array usage.
108-
*
109-
* https://stackoverflow.com/questions/7593086/why-use-non-member-begin-and-end-functions-in-c11
110-
*/
102+
// # std::begin
103+
//
104+
// # std::end
105+
//
106+
// Array usage.
107+
//
108+
// https://stackoverflow.com/questions/7593086/why-use-non-member-begin-and-end-functions-in-c11
111109
{
112110
int v1[]{0, 1, 2, 3, 4};
113111
int v2[]{5, 5, 5, 5, 5};
@@ -125,7 +123,7 @@ int main() {
125123
//std::copy(std::begin(p1) + 1, std::begin(p1) + 4, std::begin(p2) + 2);
126124
}
127125

128-
/* Therefore, a more elegant class approach that uses exact same syntax as arrays. */
126+
// Therefore, a more elegant class approach that uses exact same syntax as arrays.
129127
{
130128
std::vector<int> v1{0, 1, 2, 3, 4};
131129
std::vector<int> v2{5, 5, 5, 5, 5};
@@ -135,12 +133,11 @@ int main() {
135133
}
136134
}
137135

138-
/* # accumulate
139-
*
140-
* Sum over range with operator+
141-
*
142-
* Also has functional versions http://www.cplusplus.com/reference/numeric/accumulate/
143-
*/
136+
// # accumulate
137+
//
138+
// Sum over range with operator+
139+
//
140+
// Also has functional versions http://www.cplusplus.com/reference/numeric/accumulate/
144141
{
145142
{
146143
std::vector<int> v{2, 0, 1};
@@ -156,10 +153,9 @@ int main() {
156153
}
157154
}
158155

159-
/* # find
160-
*
161-
* Return iterator to first found element.
162-
*/
156+
// # find
157+
//
158+
// Return iterator to first found element.
163159
{
164160
std::vector<int> v{2,0,1};
165161
unsigned int pos;
@@ -177,62 +173,57 @@ int main() {
177173
assert(pos == v.size());
178174
}
179175

180-
/* # find_if
181-
*
182-
* Like find, but using an arbitrary condition on each element instead of equality.
183-
*
184-
* Consider usage with C++11 lambdas and functional.
185-
*/
176+
// # find_if
177+
//
178+
// Like find, but using an arbitrary condition on each element instead of equality.
179+
//
180+
// Consider usage with C++11 lambdas and functional.
186181
{
187182
std::vector<int> v{2, 0, 1};
188183
assert(std::find_if (v.begin(), v.end(), odd) == --v.end());
189184
}
190185

191-
/* # binary_search
192-
*
193-
* Container must be already sorted.
194-
*
195-
* Log complexity.
196-
*
197-
* Only states if the element is present or not, but does not get its position.
198-
*
199-
* If you want to get the position of those items, use `equal_range`, `lower_bound` or `upper_bound`.
200-
*/
186+
// # binary_search
187+
//
188+
// Container must be already sorted.
189+
//
190+
// Log complexity.
191+
//
192+
// Only states if the element is present or not, but does not get its position.
193+
//
194+
// If you want to get the position of those items, use `equal_range`, `lower_bound` or `upper_bound`.
201195
{
202196
std::vector<int> v{0, 1, 2};
203197
assert(std::binary_search(v.begin(), v.end(), 1) == true);
204198
assert(std::binary_search(v.begin(), v.end(), 3) == false);
205199
assert(std::binary_search(v.begin(), v.end() - 1, 2) == false);
206200
}
207201

208-
/* # lower_bound
209-
*
210-
* Finds first element in container which is not less than val.
211-
*/
202+
// # lower_bound
203+
//
204+
// Finds first element in container which is not less than val.
212205
{
213206
std::vector<int> v{0, 2, 3};
214207
auto it = std::lower_bound(v.begin(), v.end(), 1);
215208
assert(it - v.begin() == 1);
216209
}
217210

218-
/* # upper_bound
219-
*
220-
* Finds first element in container is greater than val.
221-
*/
211+
// # upper_bound
212+
//
213+
// Finds first element in container is greater than val.
222214
{
223215
std::vector<int> v{0, 1, 2};
224216
auto it = std::upper_bound(v.begin(), v.end(), 1);
225217
assert(it - v.begin() == 2);
226218
}
227219

228-
/* # equal_range
229-
*
230-
* Finds first and last location of a value iniside a ranged container.
231-
*
232-
* Return values are the same as lower_bound and upper_bound.
233-
*
234-
* log complexity.
235-
*/
220+
// # equal_range
221+
//
222+
// Finds first and last location of a value iniside a ranged container.
223+
//
224+
// Return values are the same as lower_bound and upper_bound.
225+
//
226+
// log complexity.
236227
{
237228
std::vector<int> v{0, 1, 1, 2};
238229
std::vector<int>::iterator begin, end;
@@ -257,19 +248,18 @@ int main() {
257248
assert(*std::min_element(v.begin(), v.end()) == 0);
258249
}
259250

260-
/* # advance
261-
*
262-
* Advance iterator by given number.
263-
*
264-
* If random access, simply adds + N.
265-
*
266-
* Else, calls `++` N times.
267-
*
268-
* Advantage over `+`: only random access containers support `+`,
269-
* but this works for any container, allowing one to write more general code.
270-
*
271-
* Beware however that this operation will be slow for non random access containers.
272-
*/
251+
// # advance
252+
//
253+
// Advance iterator by given number.
254+
//
255+
// If random access, simply adds + N.
256+
//
257+
// Else, calls `++` N times.
258+
//
259+
// Advantage over `+`: only random access containers support `+`,
260+
// but this works for any container, allowing one to write more general code.
261+
//
262+
// Beware however that this operation will be slow for non random access containers.
273263
{
274264
std::vector<int> v{0, 1, 2};
275265
auto it = v.begin();
@@ -278,10 +268,9 @@ int main() {
278268
}
279269

280270
#if __cplusplus >= 201103L
281-
/* # next
282-
*
283-
* Same as advance, but returns a new iterator instead of modifying the old one.
284-
*/
271+
// # next
272+
//
273+
// Same as advance, but returns a new iterator instead of modifying the old one.
285274
{
286275
std::vector<int> v{0, 1, 2};
287276
auto it(v.begin());

‎cpp/class.cpp‎

Lines changed: 17 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -903,13 +903,9 @@ int main() {
903903
904904
# Zero initialization
905905
906-
- http://en.cppreference.com/w/cpp/language/value_initialization
907-
- http://en.cppreference.com/w/cpp/language/zero_initialization
908-
- http://stackoverflow.com/questions/1613341/what-do-the-following-phrases-mean-in-c-zero-default-and-value-initializat
909-
910-
# Aggregate initialization
911-
912-
http://en.cppreference.com/w/cpp/language/aggregate_initialization
906+
- http://en.cppreference.com/w/cpp/language/value_initialization
907+
- http://en.cppreference.com/w/cpp/language/zero_initialization
908+
- http://stackoverflow.com/questions/1613341/what-do-the-following-phrases-mean-in-c-zero-default-and-value-initializat
913909
*/
914910
{
915911
// Syntax with new.
@@ -930,11 +926,9 @@ int main() {
930926
delete[] t;
931927
}
932928

933-
/*
934-
Works for objects.
935-
936-
Note how the default constructor was called since `z == 1`.
937-
*/
929+
// Works for objects.
930+
//
931+
// Note how the default constructor was called since `z == 1`.
938932
{
939933
{
940934
Class *cs = new Class[1]();
@@ -951,18 +945,16 @@ int main() {
951945
}
952946
}
953947

954-
/*
955-
# initialize built-in types
956-
957-
http://stackoverflow.com/questions/5113365/do-built-in-types-have-default-constructors
958-
959-
C++ adds new ways to initialize base types.
960-
961-
Those are not however constructors.
962-
963-
They probably just mimic constructor syntax to help blurr the distinction
964-
between built-in types and classes and make templates and typedefs work better.
965-
*/
948+
// # initialize built-in types
949+
//
950+
// http://stackoverflow.com/questions/5113365/do-built-in-types-have-default-constructors
951+
//
952+
// C++ adds new ways to initialize base types.
953+
//
954+
// Those are not however constructors.
955+
//
956+
// They probably just mimic constructor syntax to help blurr the distinction
957+
// between built-in types and classes and make templates and typedefs work better.
966958
{
967959
// Parenthesis initialization
968960
{
@@ -986,7 +978,7 @@ int main() {
986978
987979
See uniform initialization.
988980
989-
<http://stackoverflow.com/questions/14232184/initializing-scalars-with-braces>
981+
http://stackoverflow.com/questions/14232184/initializing-scalars-with-braces
990982
991983
# Uniform initialization
992984

0 commit comments

Comments
 (0)