diff --git "a/src/\347\276\244\345\217\213\346\217\220\344\272\244/\347\254\25414\351\242\230/mq\346\227\245.cpp" "b/src/\347\276\244\345\217\213\346\217\220\344\272\244/\347\254\25414\351\242\230/mq\346\227\245.cpp" new file mode 100644 index 00000000..2df6cddb --- /dev/null +++ "b/src/\347\276\244\345\217\213\346\217\220\344\272\244/\347\254\25414\351\242\230/mq\346\227\245.cpp" @@ -0,0 +1,17 @@ +#include + +namespace ss +{ + int a = 0; + namespace s + { + int& x = a; + } +} + +int main() +{ + ::ss::s::x = 100; + // todo.. + std::cout << ss::a << '\n'; +} \ No newline at end of file diff --git "a/src/\347\276\244\345\217\213\346\217\220\344\272\244/\347\254\25415\351\242\230/mq\346\227\245.cpp" "b/src/\347\276\244\345\217\213\346\217\220\344\272\244/\347\254\25415\351\242\230/mq\346\227\245.cpp" new file mode 100644 index 00000000..9c72863b --- /dev/null +++ "b/src/\347\276\244\345\217\213\346\217\220\344\272\244/\347\254\25415\351\242\230/mq\346\227\245.cpp" @@ -0,0 +1,111 @@ +#include +#include +#include +#include +#include +#include +// 为std::vector增加一个自定义的赋值函数 +template + requires std::disjunction_v, std::is_floating_point> +class vector : public std::vector +{ +public: + using std::vector::vector; + using std::vector::size; + using std::vector::operator[]; + template + vector &operator=(const E &e) + { + const auto count = std::min(size(), e.size()); + this->resize(count); + for (std::size_t idx{0}; idx < count; ++idx) + { + this->operator[](idx) = e[idx]; + } + return *this; + } +}; + +/* +// 实现表达式模板类及相关函数 +template<...> +struct vector_expr { + +}; + +// operator+ +// operator- +// operator* +// operator/ +*/ + +template +struct vector_expr +{ + F f; + const Expr1& expr1; + const Expr2& expr2; + vector_expr(Expr1 &expr1, Expr2 &expr2) :expr1(expr1), expr2(expr2) {} + auto size() const + { + return std::min(expr1.size(), expr2.size()); + } + auto operator[](std::size_t idx) const + { + return f(expr1[idx], expr2[idx]); + } +}; + +template +auto operator+(const Expr1 &lhs, const Expr2 &rhs) +{ + return vector_expr, const Expr1, const Expr2>(lhs, rhs); +} + +template +auto operator-(const Expr1 &lhs, const Expr2 &rhs) +{ + return vector_expr, const Expr1, const Expr2>(lhs, rhs); +} + +template +auto operator*(const Expr1 &lhs, const Expr2 &rhs) +{ + return vector_expr, const Expr1, const Expr2>(lhs, rhs); +} + +template +auto operator/(const Expr1 &lhs, const Expr2 &rhs) +{ + return vector_expr, const Expr1, const Expr2>(lhs, rhs); +} +//auto operator + +int main() +{ + auto print = [](const auto &v) + { + std::ranges::copy(v, std::ostream_iterator>{std::cout, ", "}); + std::cout << std::endl; + }; + const vector a{1.2764, 1.3536, 1.2806, 1.9124, 1.8871, 1.7455}; + const vector b{2.1258, 2.9679, 2.7635, 2.3796, 2.4820, 2.4195}; + const vector c{3.9064, 3.7327, 3.4760, 3.5705, 3.8394, 3.8993}; + const vector d{4.7337, 4.5371, 4.5517, 4.2110, 4.6760, 4.3139}; + const vector e{5.2126, 5.1452, 5.8678, 5.1879, 5.8816, 5.6282}; + + { + vector result(6); + for (std::size_t idx = 0; idx < 6; idx++) + { + result[idx] = a[idx] - b[idx] * c[idx] / d[idx] + e[idx]; + } + print(result); + } + { + vector result(6); + result = a - b * c / d + e; // 使用表达式模板计算 + print(result); + } + return 0; +} \ No newline at end of file diff --git "a/src/\347\276\244\345\217\213\346\217\220\344\272\244/\347\254\25416\351\242\230/mq\346\227\245.cpp" "b/src/\347\276\244\345\217\213\346\217\220\344\272\244/\347\254\25416\351\242\230/mq\346\227\245.cpp" new file mode 100644 index 00000000..c704dfee --- /dev/null +++ "b/src/\347\276\244\345\217\213\346\217\220\344\272\244/\347\254\25416\351\242\230/mq\346\227\245.cpp" @@ -0,0 +1,15 @@ +#include + +#define BY_NAME(func) [](auto &&...args) { return func(std::forward(args)...); } + +template +auto foo(F f, Args &&...args)requires (std::is_invocable_v) +{ + return f(std::forward(args)...); +} + +int main() +{ + const auto result = foo(BY_NAME(std::min), 2, 3); + std::cout << result << '\n'; +} \ No newline at end of file