-
Notifications
You must be signed in to change notification settings - Fork 29
/
stl_11_generate_n.cpp
77 lines (62 loc) · 1.47 KB
/
stl_11_generate_n.cpp
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
// code_report
// https://youtu.be/Jr6e8oanDrg
#include <vector>
#include <algorithm>
#include <numeric>
#include <iostream>
#include <string>
using namespace std;
const int N = 10;
template<typename T>
void print(T t) {
for (auto e : t) cout << e << ' ';
cout << endl;
}
void use_generate_abc() {
vector<string> v(5);
generate(begin(v), end(v), [s = ""s, c = '`']() mutable { ++c; s += c; return s; });
print(v);
}
void use_generate_n_abc() {
vector<string> v;
generate_n(back_inserter(v), 5, [s = ""s, c = '`']() mutable { ++c; s += c; return s; });
print(v);
}
void use_iota() {
vector<int> v(N);
iota(begin(v), end(v), 1);
print(v);
}
void use_generate() {
vector<int> v(N);
generate(begin(v), end(v), [i = 0]() mutable { ++i; return i; });
print(v);
}
void use_generate_n() {
vector<int> v;
generate_n(back_inserter(v), N, [i = 0]() mutable { ++i; return i; });
print(v);
}
void use_generate_n_with_reserve() {
vector<int> v;
v.reserve(N);
generate_n(back_inserter(v), N, [i = 0]() mutable { ++i; return i; });
print(v);
}
void use_generate_n_with_reserve_with_resize() {
vector<int> v;
v.reserve(N);
v.resize(N);
generate_n(begin(v), N, [i = 0]() mutable { ++i; return i; });
print(v);
}
int main () {
use_generate_abc();
use_generate_n_abc();
use_iota();
use_generate();
use_generate_n();
use_generate_n_with_reserve();
use_generate_n_with_reserve_with_resize();
return 0;
}