C++ Library Extensions 2022.12.09
To help learn modern C++ programming
19-containers.cpp
Go to the documentation of this file.
1#include <tpf_output.hpp>
2
5auto nl = tpf::nl;
6
7namespace types = tpf::types;
8
10{
11 using coin_t = int;
12 using money_t = double;
13 using title_t = const char*;
14
16 using item_t = bag_t::variant_type;
17
18 bag_t my_bag; // vector of variants
19
20 my_bag.emplace_back(10);
21 my_bag.emplace_back(100.23);
22 my_bag.emplace_back("Modern C++ Programming");
23 my_bag.emplace_back("C++17 Standard");
24
25 stream <<"Contents of my bag: " << my_bag << endl;
26
27 auto handle_coin = [](const coin_t& coin_count)
28 {
29 stream <<"There are " << coin_count <<" coins."<<nl;
30 };
31
32 auto handle_money = [](const money_t& money)
33 {
34 stream <<"I have " << money <<" bucks." << nl;
35 };
36
37 auto handle_title = [](title_t& title)
38 {
39 stream <<"I like reading \"" << title << "\"."<<nl;
40 };
41
42 types::overloads handle_my_bag { handle_coin, handle_money, handle_title};
43
44 auto handle_items = [&handle_my_bag](auto& item)
45 {
46 std::visit(handle_my_bag, item);
47 };
48
49 stream <<"Items in my bag, item by item: "<< endl;
50
51 for(auto& item: my_bag)
52 {
53 handle_items(item);
54 }
55
56 stream << endl;
57}
58
60{
61 using coin_t = int;
62 using money_t = double;
63 using title_t = const char*;
64
66 using item_t = bag_t::variant_type;
67
68 bag_t my_bag; // vector of variants
69
70 my_bag.emplace_back(10);
71 my_bag.emplace_back(100.23);
72 my_bag.emplace_back("Modern C++ Programming");
73 my_bag.emplace_back("C++17 Standard");
74
75 stream <<"Contents of my bag: " << my_bag << endl;
76
77 types::overloads handle_my_bag
78 {
79 [](const coin_t& coin_count)
80 {
81 stream <<"There are " << coin_count <<" coins."<<nl;
82 },
83
84 [](const money_t& money)
85 {
86 stream <<"I have " << money <<" bucks." << nl;
87 },
88
89 [](title_t& title)
90 {
91 stream <<"I like reading \"" << title << "\"."<<nl;
92 }
93
94 }; // constructor of types::overloads
95
96 stream <<"Items in my bag, item by item: "<< endl;
97
98 for(auto& item: my_bag)
99 {
100 handle_my_bag.handle(item);
101 }
102
103 stream << endl;
104}
105
107{
108 using coin_t = int;
109 using money_t = double;
110 using title_t = const char*;
111
113 using item_t = bag_t::variant_type;
114
115 bag_t my_bag; // vector of variants
116
117 my_bag.emplace_back(10);
118 my_bag.emplace_back(100.23);
119 my_bag.emplace_back("Modern C++ Programming");
120 my_bag.emplace_back("C++17 Standard");
121
122 types::overloads handle_my_bag
123 {
124 [](const coin_t& coin_count)
125 {
126 stream <<"There are " << coin_count <<" coins."<<nl;
127 },
128
129 [](const money_t& money)
130 {
131 stream <<"I have " << money <<" bucks." << nl;
132 },
133
134 [](title_t& title)
135 {
136 stream <<"I like reading \"" << title << "\"."<<nl;
137 }
138
139 }; // constructor of types::overloads
140
141 stream <<"Items in my bag, item by item, in in-order: "<< endl;
142
143 handle_my_bag.for_each(my_bag);
144
145 stream << endl;
146
147 stream <<"Items in my bag, item by item, in reverse-order: "<< endl;
148
149 handle_my_bag.for_each_reverse(my_bag);
150
151 stream << endl;
152}
153
155{
156 using age_t = int;
157 using weight_t = double;
158 using name_t = const char*;
159
161 using person_t = people_t::tuple_type;
162
163 people_t friends;
164
165 friends.emplace_back(18, 56.5, "Alice Kim");
166 friends.emplace_back(17, 70.6, "Seven Park");
167 friends.emplace_back(16, 45.7, "Michelle Lee");
168
169 stream <<"My friends: " << friends << endl;
170
171 stream <<"\nEach of my friends: " << endl;
172 for(auto& f: friends)
173 {
174 stream << f << endl;
175 }
176
177 stream << endl;
178
179 stream <<"\nEach properties of my individual friend: " << endl;
180 for(auto& f: friends)
181 {
182 auto [age, weight, name] = f;
183
184 stream << "My name is " << name << endl;
185 stream << "I am " << age << " years old."<<endl;
186 stream << "I weigh " <<weight << " kg. " << endl << endl;
187 }
188
189 stream << endl;
190}
191
192int main()
193{
194 // test_container_of_variants();
195
196 // test_container_of_variants_simplified();
197
198 // test_container_of_variants_further_simplified();
199
201}
void test_container_of_variants_simplified()
tpf::sstream stream
void test_container_of_variants()
auto endl
void test_container_of_variants_further_simplified()
auto nl
int main()
void test_container_of_tuples()
Type to string name conversions are defined.
Definition: 31-visit.cpp:7
hidden::vector_of_variants_t< ElementTypes... > vector_of_variants_t
Definition: tpf_types.hpp:6621
enable_if_variant_t< VariantType > visit(VisitorType &&visitor, VariantType &&vt)
Definition: 31-visit.cpp:118
hidden::deque_of_tuples_t< ElementTypes... > deque_of_tuples_t
Definition: tpf_types.hpp:7004
constexpr auto endl
Definition: tpf_output.hpp:973
constexpr auto nl
Definition: tpf_output.hpp:971
Stream output operators << are implemented.