|
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667 |
- #include "../checker.h"
- #include <vector>
-
- // READ: 左值右值(概念)<https://learn.microsoft.com/zh-cn/cpp/c-language/l-value-and-r-value-expressions?view=msvc-170>
- // READ: 左值右值(细节)<https://zh.cppreference.com/w/cpp/language/value_category>
- // READ: 关于移动语义 <https://learn.microsoft.com/zh-cn/cpp/cpp/rvalue-reference-declarator-amp-amp?view=msvc-170#move-semantics>
- // READ: 如果实现移动构造 <https://learn.microsoft.com/zh-cn/cpp/cpp/move-constructors-and-move-assignment-operators-cpp?view=msvc-170>
-
- // READ: 移动构造函数 <https://zh.cppreference.com/w/cpp/language/move_constructor>
- // READ: 移动赋值 <https://zh.cppreference.com/w/cpp/language/move_assignment>
- // READ: 运算符重载 <https://zh.cppreference.com/w/cpp/language/operators>
-
- class DynFibonacci {
- size_t *cache;
- int cached;
-
- public:
- // TODO: 实现动态设置容量的构造器
- DynFibonacci(int capacity) {}
-
- // TODO: 实现移动构造器
- DynFibonacci(DynFibonacci && other) {
-
- };
-
- // TODO: 实现移动赋值
- // NOTICE: ⚠ 注意移动到自身问题 ⚠
- DynFibonacci &operator=(DynFibonacci && other) noexcept {
- // ASK AI: 这里的 noexcept 是什么含义?
- }
-
- // TODO: 实现正确的缓存优化斐波那契计算
- // 我们重载了下标运算符,请给出正确的实现
- size_t operator[](int i) {
-
- }
-
- // NOTICE: 不要修改这个方法
- // THINK: 这个方法和上面的方法有什么不同?
- size_t operator[](int i) const {
- ASSERT(i <= cached, "i out of range");
- return cache[i];
- }
-
- // NOTICE: 不要修改这个方法
- bool is_alive() const {
- return cache;
- }
- };
-
- int main(int argc, char **argv) {
- DynFibonacci fib(12);
- ASSERT(fib[10] == 55, "fibonacci(10) should be 55");
-
- DynFibonacci const fib_ = std::move(fib);
- ASSERT(!fib.is_alive(), "Object moved");
- ASSERT(fib_[10] == 55, "fibonacci(10) should be 55");
-
- DynFibonacci fib0(6);
- DynFibonacci fib1(12);
-
- fib0 = std::move(fib1);
- fib0 = std::move(fib0);
- ASSERT(fib0[10] == 55, "fibonacci(10) should be 55");
-
- return 0;
- }
|