-
Notifications
You must be signed in to change notification settings - Fork 137
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* CodeyZ-first-pr 1~3题
- Loading branch information
Showing
3 changed files
with
89 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,21 @@ | ||
#include<iostream> | ||
#include<vector> | ||
#include <functional> | ||
|
||
template<typename V,typename T> | ||
auto operator|(V&& v, T&& f) { | ||
for (auto &i : v) { | ||
f(i); | ||
} | ||
return v; | ||
} | ||
|
||
|
||
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; | ||
return 0; | ||
} |
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,32 @@ | ||
#include<iostream> | ||
#include<format> | ||
#include<string.h> | ||
#include<numbers> | ||
|
||
struct display{ | ||
private: | ||
const char* m_s; | ||
public: | ||
display(const char* s) :m_s(s) {} | ||
|
||
template<typename...Args> | ||
auto operator()(Args...args) { | ||
return std::vformat(m_s, std::make_format_args(std::forward<Args>(args)...)); | ||
} | ||
}; | ||
|
||
auto operator""_f(const char* s,std::size_t n) { | ||
return display{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); | ||
return 0; | ||
} |
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,36 @@ | ||
#include<iostream> | ||
#include<format> | ||
#include<string.h> | ||
#include<string_view> | ||
struct Frac { | ||
int a, b; | ||
}; | ||
|
||
template<> | ||
struct std::formatter<Frac, char> { | ||
|
||
template<class ParseContext> | ||
constexpr auto parse(ParseContext& ctx) { | ||
auto it = ctx.begin(); | ||
if (it == ctx.end()) | ||
return it; | ||
if (it != ctx.end() && *it != '}') | ||
throw std::format_error("无效的 QuotableString 格式参数。"); | ||
return it; | ||
} | ||
template<class Fmtcontext> | ||
auto format(::Frac f, Fmtcontext& ctx)const { | ||
return format_to(ctx.out(), "{}/{}", f.a, f.b); | ||
} | ||
}; | ||
|
||
template<typename...Args> | ||
auto print(std::string_view s,Args&&...args) { | ||
std::cout << std::vformat(s,std::make_format_args(std::forward<Args>(args)...)) << '\n'; | ||
} | ||
|
||
int main() { | ||
Frac f{ 1,10 }; | ||
print("{}", f);// 结果为1/10 | ||
//std::cout << std::format("{}", f); | ||
} |