Skip to content

Commit

Permalink
Merge pull request #216 from MrShinshi/main
Browse files Browse the repository at this point in the history
作业补档 01 02 03 04 05
  • Loading branch information
Mq-b authored Dec 30, 2023
2 parents b8ff2ed + cc6e5cd commit 45d7866
Show file tree
Hide file tree
Showing 6 changed files with 251 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/群友提交/第01题/心洗.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import std;

template<typename C, typename F>
auto operator|(C&& container, F&& func)
{
for (auto&& item : container)
func(item);

return container;
}
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;
}
40 changes: 40 additions & 0 deletions src/群友提交/第01题/金兔兔.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import std;

template<typename T>
struct Lazy
{
using FnType = std::function<void(T&)>;
std::vector<T> v;
mutable FnType map;

Lazy(std::vector<T> v) noexcept :v(v), map([](T& x) {}) {}
Lazy(std::vector<T> v, FnType map) noexcept :v(v), map(map) {}
~Lazy() noexcept
{
for (auto& x : v)
map(x);
}
const Lazy& operator|(FnType map2) const noexcept
{
auto old = map;
map = [map2, old](T& x) {
old(x);
map2(x);
};
return *this;
}
};

template<typename T, typename F>
Lazy<T> operator|(const std::vector<T>& c, const F& map) noexcept
{
return Lazy{ c } | std::function<void(T&)>(map);
}

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;
}
47 changes: 47 additions & 0 deletions src/群友提交/第02题/心洗.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import std;

#define USE_LAMBDA
#ifdef USE_LAMBDA
struct format
{
format(const char* s) :m_const_ch(s)
{

}

template<typename... Args>
auto operator()(Args... args)
{
return std::vformat(m_const_ch, std::make_format_args(args...));
}

private:
std::string_view m_const_ch;
};

auto operator ""_f(const char* text, size_t)
{
return format(text);
}

#else // another implementation
auto operator ""_f(const char* text, size_t)
{
return [text](auto&&... args) {
return std::vformat(text, std::make_format_args(std::forward<decltype(args)>(args)...));
};
}
#endif

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;
}
30 changes: 30 additions & 0 deletions src/群友提交/第03题/心洗.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import std;

struct Frac
{
int a;
int b;
};

template<>
struct std::formatter<Frac, char> : std::formatter<char>
{
template<typename Out>
constexpr auto format(const Frac& frac, std::basic_format_context<Out, char>& context) const
{
return std::format_to(context.out(), "{}/{}", frac.a, frac.b);
}
};

void print(auto&& text, auto&&... args)
{
std::cout << std::vformat(
std::forward<decltype(text)>(text),
std::make_format_args(std::forward<decltype(args)>(args)...));
}

int main()
{
print(std::string("{}"), Frac f{ 1, 10 });
return 0;
}
33 changes: 33 additions & 0 deletions src/群友提交/第04题/心洗.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import std;

class ComponentBase {
protected:
static inline size_t component_type_count = 0;
};

template <typename T>
class Component : public ComponentBase {
public:
static auto component_type_id() {
static size_t component_type_id = component_type_count++;
return component_type_id;
}
};

class A : public Component<A> {};

class B : public Component<B> {};

class C : public Component<C> {};

#include <iostream>

int main(int argc, char* argv[]) {
std::cout << "A::id() = " << A::component_type_id() << std::endl;
std::cout << "B::id() = " << B::component_type_id() << std::endl;
std::cout << "B::id() = " << B::component_type_id() << std::endl;
std::cout << "A::id() = " << A::component_type_id() << std::endl;
std::cout << "A::id() = " << A::component_type_id() << std::endl;
std::cout << "C::id() = " << C::component_type_id() << std::endl;
return 0;
}
84 changes: 84 additions & 0 deletions src/群友提交/第05题/心洗.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import std;

template <typename... T>
requires std::invocable<T...>
class scope_guard {
public:
constexpr explicit scope_guard(T&&... args)
: package_task(std::bind(
std::forward<decltype(args)>(args)...))
{
}

~scope_guard() noexcept
{
package_task();
}

private:
const std::function<void()> package_task;
};

template <class Func, class... Args>
scope_guard(Func&&, Args&&...) -> scope_guard<Func&&, Args&&...>;

struct X {
X()
{
std::cout << "X()\n";
}

X(const X&)
{
std::cout << "X(const X&)\n";
}

X(X&&) noexcept
{
std::cout << "X(X&&)\n";
}

~X()
{
std::cout << "~X()\n";
}
};

int main()
{
{
auto x = new X{};
auto guard = scope_guard([&] {
delete x;
x = nullptr;
});
}
std::cout << "----------\n";
{
struct Test {
void operator()(X*& x)
{
delete x;
x = nullptr;
}
};

auto x = new X{};
Test t;
auto guard = scope_guard(t, x);
}
std::cout << "----------\n";
{
struct Test {
void f(X*& x)
{
delete x;
x = nullptr;
}
};

auto x = new X{};
Test t;
auto guard = scope_guard{ &Test::f, &t, x }; // error
}
}

0 comments on commit 45d7866

Please sign in to comment.