@@ -23,6 +23,7 @@ descriptive and reduces chances of making mistakes. It is generally easier to re
23
23
| ` c_foreach_n (it, ctype, container, n) ` | Iteratate ` n ` first elements. Index variable is ` {it}_index ` . |
24
24
| ` c_foreach_kv (key, val, ctype, container) ` | Iterate maps with "structured binding" |
25
25
<!-- {%raw%}-->
26
+ [ [ Run this code] ( https://godbolt.org/z/r8eWG4Txa ) ]
26
27
``` c++
27
28
#define i_type IMap, int, int
28
29
#include "stc/smap.h"
@@ -133,6 +134,8 @@ crange& c_iota(start, stop); // l-value; otherwise like crange_ma
133
134
crange& c_iota(start, stop, step); // l-value; otherwise like crange_make(start, stop, step)
134
135
```
135
136
The ** crange_value** type is * isize* . Variables * start* , * stop* , and * step* are of type * crange_value* .
137
+
138
+ [ [ Run this code] ( https://godbolt.org/z/6aaq6qTro ) ]
136
139
``` c++
137
140
// 1. All primes less than 32: See below for c_filter() and is_prime()
138
141
crange r1 = crange_make(3 , 32 , 2 );
@@ -145,10 +148,10 @@ c_filter(crange, r1, true
145
148
146
149
// 2. The first 11 primes:
147
150
// c_iota() can be used as argument to c_filter.
148
- printf ("2");
151
+ printf ("2"); // first prime
149
152
c_filter(crange, c_iota(3), true
150
153
&& is_prime(* value)
151
- && (printf(" %zi", * value), c_flt_take(10 ))
154
+ && (c_flt_take(10), printf(" %zi", * value))
152
155
);
153
156
// 2 3 5 7 11 13 17 19 23 29 31
154
157
```
@@ -365,6 +368,7 @@ int main(void) {
365
368
A macro for conveniently defining functions with multiple return values. This is for encouraging
366
369
to write functions that returns extra error context when error occurs, or just multiple return values.
367
370
371
+ [ [ Run this code] ( https://godbolt.org/z/MsYG75Eae ) ]
368
372
``` c++
369
373
Vec get_data (void) {
370
374
return c_make(Vec, {1, 2, 3, 4, 5, 6});
@@ -475,6 +479,8 @@ Append linearily in containers using a predicate. `value` is a pointer to each e
475
479
Erase linearily in containers using a predicate. ` value ` is a pointer to each element in predicate.
476
480
- ` c_erase_if(CntType, cnt_ptr, pred) ` . Use with ** list** , ** hmap** , ** hset** , ** smap** , and ** sset** .
477
481
- ` c_eraseremove_if(CntType, cnt_ptr, pred) ` . Use with ** stack** , ** vec** , ** deque** , and ** queue** only.
482
+
483
+ [ [ Run this code] ( https://godbolt.org/z/5WWGf4dbd ) ]
478
484
<!-- {%raw%}-->
479
485
``` c++
480
486
#include < stdio.h>
@@ -483,7 +489,7 @@ Erase linearily in containers using a predicate. `value` is a pointer to each el
483
489
484
490
#define i_type Vec, int
485
491
#define i_use_cmp
486
- #include "stc/vec .h"
492
+ #include "stc/stack .h"
487
493
488
494
#define i_type List, int
489
495
#define i_use_cmp
@@ -506,7 +512,6 @@ int main(void)
506
512
507
513
// Search vec for first value > 20.
508
514
Vec_iter result;
509
-
510
515
c_find_if(Vec, vec, &result, *value > 20);
511
516
if (result.ref) printf("found %d\n", *result.ref);
512
517
@@ -589,18 +594,19 @@ The *X_sort()*, *X_sort_lowhigh()* functions are about twice as fast as *qsort()
589
594
speed with *std::sort()**. Both *X_binary_seach()* and *X_lower_bound()* are about 30% faster than
590
595
c++ *std::lower_bound()*.
591
596
##### Usage examples
597
+
598
+ [ [Run this code](https://godbolt.org/z/rr1xvjcGG) ]
592
599
```c++
593
600
#define i_key int // sort a regular c-array of ints
594
601
#include "stc/sort.h"
595
602
#include <stdio.h>
596
603
597
604
int main(void) {
598
- int nums [] = {5, 3, 5, 9, 7, 4, 7, 2, 4, 9, 3, 1, 2, 6, 4};
599
- ints_sort(nums , c_arraylen(nums )); // `ints` derived from the `i_key` name
605
+ int arr [] = {5, 3, 5, 9, 7, 4, 7, 2, 4, 9, 3, 1, 2, 6, 4};
606
+ ints_sort(arr , c_arraylen(arr )); // `ints` derived from the `i_key` name
600
607
c_forrange (i, c_arraylen(arr)) printf(" %d", arr[i]);
601
608
}
602
609
```
603
-
604
610
``` c++
605
611
#define i_type MyDeq, int
606
612
#define i_use_cmp // enable sorting
0 commit comments