Skip to content

Commit 880a910

Browse files
authored
Merge pull request #226 from Mq-b/hw15
卢瑟作业第15题(#159
2 parents 2fba757 + 84ea885 commit 880a910

File tree

1 file changed

+84
-0
lines changed

1 file changed

+84
-0
lines changed

README.md

+84
Original file line numberDiff line numberDiff line change
@@ -1979,3 +1979,87 @@ int main() {
19791979
当某个命名空间的成员变量在该命名空间外被定义时,该定义中用到的名字的查找会以与在命名空间之内使用的名字相同的方式进行。
19801980
19811981
参见[文档](https://zh.cppreference.com/w/cpp/language/unqualified_lookup#.E5.9C.A8.E5.91.BD.E5.90.8D.E7.A9.BA.E9.97.B4.E5.A4.96.E8.BF.9B.E8.A1.8C.E5.AE.9A.E4.B9.89)。
1982+
1983+
## `15` 表达式模板
1984+
1985+
日期:`2024/1/7` 出题人:[`Matrix-A`](https://github.com/Matrix-A)([#159](https://github.com/Mq-b/Loser-HomeWork/issues/159))
1986+
1987+
1. 使用**表达式模板**补全下面的代码,实现表达式计算;
1988+
2. 指出**表达式模板**和 STL Ranges 库中哪些**视图**类似,并指出它们的异同和优缺点。
1989+
1990+
```cpp
1991+
#include <algorithm>
1992+
#include <functional>
1993+
#include <print>
1994+
#include <ranges>
1995+
#include <vector>
1996+
1997+
// 为std::vector增加一个自定义的赋值函数
1998+
template <typename T>
1999+
requires std::disjunction_v<std::is_integral<T>, std::is_floating_point<T>>
2000+
class vector : public std::vector<T> {
2001+
public:
2002+
using std::vector<T>::vector;
2003+
using std::vector<T>::size;
2004+
using std::vector<T>::operator[];
2005+
template <typename E>
2006+
vector<T>& operator=(const E& e)
2007+
{
2008+
const auto count = std::min(size(), e.size());
2009+
this->resize(count);
2010+
for (std::size_t idx { 0 }; idx < count; ++idx) {
2011+
this->operator[](idx) = e[idx];
2012+
}
2013+
return *this;
2014+
}
2015+
};
2016+
2017+
/*
2018+
// 实现表达式模板类及相关函数
2019+
template<...>
2020+
struct vector_expr {
2021+
2022+
};
2023+
2024+
// operator+
2025+
// operator-
2026+
// operator*
2027+
// operator/
2028+
*/
2029+
2030+
int main()
2031+
{
2032+
auto print = [](const auto& v) {
2033+
std::for_each(std::begin(v), std::end(v), [](const auto& e) {
2034+
std::print("{}, ", e);
2035+
});
2036+
std::println("");
2037+
};
2038+
const vector<double> a { 1.2764, 1.3536, 1.2806, 1.9124, 1.8871, 1.7455 };
2039+
const vector<double> b { 2.1258, 2.9679, 2.7635, 2.3796, 2.4820, 2.4195 };
2040+
const vector<double> c { 3.9064, 3.7327, 3.4760, 3.5705, 3.8394, 3.8993 };
2041+
const vector<double> d { 4.7337, 4.5371, 4.5517, 4.2110, 4.6760, 4.3139 };
2042+
const vector<double> e { 5.2126, 5.1452, 5.8678, 5.1879, 5.8816, 5.6282 };
2043+
2044+
{
2045+
vector<double> result(6);
2046+
for (std::size_t idx = 0; idx < 6; idx++) {
2047+
result[idx] = a[idx] - b[idx] * c[idx] / d[idx] + e[idx];
2048+
}
2049+
print(result);
2050+
}
2051+
{
2052+
vector<double> result(6);
2053+
result = a - b * c / d + e; // 使用表达式模板计算
2054+
print(result);
2055+
}
2056+
return 0;
2057+
}
2058+
```
2059+
2060+
学习链接:
2061+
2062+
- [Wikipedia - Expression templates](https://en.wikipedia.org/wiki/Expression_templates)
2063+
- [我们不需要臭名昭著的表达式模板(英文)](https://gieseanw.wordpress.com/2019/10/20/we-dont-need-no-stinking-expression-templates/)
2064+
- [C++语言的表达式模板:表达式模板的入门性介绍](https://blog.csdn.net/magisu/article/details/12964911)
2065+
- [std::valarray](https://zh.cppreference.com/w/cpp/numeric/valarray) 在一些 STL 实现中使用了表达式模板

0 commit comments

Comments
 (0)