forked from Mq-b/Loser-HomeWork
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
68 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
#include<iostream> | ||
#include<vector> | ||
#include<functional> | ||
|
||
template<typename T, typename Formatter> | ||
T &operator|(T &arr, const Formatter &func) { | ||
std::for_each(std::begin(arr), std::end(arr), func); | ||
return arr; | ||
} | ||
|
||
int main() { | ||
std::vector v{ 1, 2, 3 }; | ||
std::function f{ [](const int &i) {std::cout << i << ' '; } }; | ||
auto f2 = [](int &i) {i *= i; }; | ||
v | f2 | f; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
#include<iostream> | ||
#include<numbers> | ||
#include<format> | ||
|
||
struct Formatter { | ||
const std::string_view fmt; | ||
|
||
Formatter(const char *s) : fmt(s) { } | ||
|
||
template<typename ...T> | ||
std::string operator()(const T &...args) const { | ||
return std::vformat(fmt, std::make_format_args(args...)); | ||
} | ||
}; | ||
|
||
Formatter operator""_f(const char *s, std::size_t) { | ||
return { s }; | ||
} | ||
|
||
int main() { | ||
std::cout << "乐 :{} *\n"_f(5); | ||
std::cout << "乐 :{0} {0} *\n"_f(5); | ||
std::cout << "乐 :{:b} *\n"_f(0b01010101); | ||
std::cout << "{:*<10}"_f("卢瑟"); | ||
std::cout << '\n'; | ||
int n{}; | ||
std::cin >> n; | ||
std::cout << "π:{:.{}f}\n"_f(std::numbers::pi_v<double>, n); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
#include<iostream> | ||
#include<format> | ||
|
||
struct Frac { | ||
int a, b; | ||
}; | ||
|
||
template<> | ||
struct std::formatter<Frac> : std::formatter<char> { | ||
auto format(const Frac &arg, std::format_context &ctx) const { | ||
return std::format_to(ctx.out(), "{}/{}", arg.a, arg.b); | ||
} | ||
}; | ||
|
||
template<typename T, typename ...U> | ||
void print(T fmt, U ...args) { | ||
std::cout << std::vformat(fmt, std::make_format_args(args...)); | ||
} | ||
|
||
int main() { | ||
Frac f{ 1, 10 }; | ||
print("{}", f); | ||
} |