-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdemo_misc.cpp
More file actions
180 lines (143 loc) · 3.86 KB
/
demo_misc.cpp
File metadata and controls
180 lines (143 loc) · 3.86 KB
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
/**
* @file demo_misc.cpp
* misc demo of cpp-fstring for STL variables
*
* Examples of:
* 1. Format Specifiers
* 2. Dates
* 3. Expressions
* 4. Ranges
*
* @ingroup examples
*
* @author Sandeep M
* @copyright Copyright 2023 Sandeep M<[email protected]> All rights reserved.
* @license MIT License
*/
#define _USE_MATH_DEFINES
#undef __STRICT_ANSI__
#include <chrono>
#include <cmath>
#include <ctime> // for tm
#include <cwchar> // for tm
#include <iostream> // for operator<<, cout, basic_ostream
#include <list>
#include <map> // for operator!=, map
#include <string> // for basic_string, char_traits, operator""s, operator<, string_literals
#include <tuple> // for tuple
#include <valarray> // for __val_expr, pow, begin, end, operator+, valarray
#include <variant> // for variant
#include <vector> // for vector
#include "fmt/chrono.h"
#include "fstr.h"
#include "utils.h"
using namespace std::string_literals;
enum class vtypes {INT, FLOAT, STRING, CHAR};
int main()
{
using std::cout;
print_info(__FILE__, __TIMESTAMP__);
std::string str;
int num = 97;
cout << R"(
Overview of f-strings in C++
============================
1. Format Specifiers
--------------------
+----------------------+
| int | {num: >8d} |
+=========+============+
| {{{num}:#b}} | {num:#08b} |
+---------+------------+
| {{{num}:c}} | {num: >8c} |
+---------+------------+
| {{{num}:d}} | {num: 8d} |
+---------+------------+
| {{{num}:o}} | {num: 8o} |
+---------+------------+
| {{{num}:x}} | {num: 8x} |
+---------+------------+
+----------------------------------+
| float | {M_PI} |
+==================================+
| {{M_PI:a}} | {M_PI: <20a} |
+----------------------------------+
| {{M_PI:e}} | {M_PI: <20e} |
+----------------------------------+
| {{M_PI:g}} | {M_PI: <20g} |
+----------------------------------+
| {{M_PI:f}} | {M_PI: <20f} |
+----------------------------------+
)";
struct tm time = {.tm_mday = 1, .tm_year = 2023 - 1900};
// int large = 10'000'000;
cout << R"(
2. Dates
---------
It was a sunny {{time:%A}} in {{time:%B}} around {{time:%OI}}{{time:%p}}
becomes:
It was a sunny {time:%A} in {time:%B} around {time:%OI}{time:%p}
)";
cout << R"(
3. Expressions
--------------
)";
auto va = std::vector<float>{5, M_PI, 10'000'000};
int xb = 10;
for (auto& ia : va) {
cout << R"(
when a={ia}, b={xb} =>
{ia} + {xb} = {ia+xb}
{ia} * {xb} = {ia*xb}
{ia} ^ {xb} = {std::pow(ia, xb)}
)"; // cout
}
int arr[] = {3, 2, 1};
auto v1 = std::vector<int>{
1,
2,
3,
};
auto v2 = std::vector<std::vector<int>>{{1, 2}, {3, 5}, {7, 11}};
std::vector<std::vector<int>> v3 = {{1, 2, 3}, {4, 5}, {6}};
auto m1 = std::map<std::string, int>{{"one", 1}, {"two", 2}};
std::map<int, std::vector<int>> m2 = {{1, {1, 2}}, {2, {3, 4}}};
auto p1 = std::pair<int, float>(42, 1.5F);
std::tuple<double, char, std::string> t1 = {3.8, 'A', "Lisa Simpson"};
cout << R"(
4. Ranges
---------
Vectors:
vector<int> {v1=}
vector<vector<int>> {v2=}
vector<vector<int>> {v3=}
Pairs:
pair<int, float> {p1=}
Maps:
map<string, int> {m1=}
map<int, vector<int>> {m2=}
Tuples:
tuple<double, char, string> {t1=}
)";
using Var = std::variant<int, float, std::string, char>;
std::map<vtypes,Var> vmap = {{vtypes::INT, 10}, {vtypes::FLOAT, 3.14f}, {vtypes::STRING, std::string("Hello")}, {vtypes::CHAR, 'a'}};
cout << R"(
Variants:
{vmap=}
)";
using IArr = std::valarray<int>;
IArr a{1, 2, 3};
IArr b{4, 5, 6};
IArr ab = std::pow(a, b);
IArr ba = std::pow(b, a);
IArr abba = ab + ba;
cout << R"(
Valarray:
a^b + b^a = {a}^{b} + {b}^{a}
= {ab} + {ba}
= {abba}
min({abba}) = {abba.min()}
sum({abba}) = {abba.sum()}
max({abba}) = {abba.max()}
)";
}