Skip to content

Commit

Permalink
涼風青葉提交第1,2,3题
Browse files Browse the repository at this point in the history
  • Loading branch information
Suzukaze7 committed Jan 29, 2024
1 parent 6e05816 commit fcfeb6d
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/群友提交/第01题/涼風青葉.cpp
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;
}
29 changes: 29 additions & 0 deletions src/群友提交/第02题/涼風青葉.cpp
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);
}
23 changes: 23 additions & 0 deletions src/群友提交/第03题/涼風青葉.cpp
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);
}

0 comments on commit fcfeb6d

Please sign in to comment.