You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

main.cc 2.2 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. #include "../checker.h"
  2. #include <vector>
  3. // READ: 左值右值(概念)<https://learn.microsoft.com/zh-cn/cpp/c-language/l-value-and-r-value-expressions?view=msvc-170>
  4. // READ: 左值右值(细节)<https://zh.cppreference.com/w/cpp/language/value_category>
  5. // READ: 关于移动语义 <https://learn.microsoft.com/zh-cn/cpp/cpp/rvalue-reference-declarator-amp-amp?view=msvc-170#move-semantics>
  6. // READ: 如果实现移动构造 <https://learn.microsoft.com/zh-cn/cpp/cpp/move-constructors-and-move-assignment-operators-cpp?view=msvc-170>
  7. // READ: 移动构造函数 <https://zh.cppreference.com/w/cpp/language/move_constructor>
  8. // READ: 移动赋值 <https://zh.cppreference.com/w/cpp/language/move_assignment>
  9. // READ: 运算符重载 <https://zh.cppreference.com/w/cpp/language/operators>
  10. class DynFibonacci {
  11. size_t *cache;
  12. int cached;
  13. public:
  14. // TODO: 实现动态设置容量的构造器
  15. DynFibonacci(int capacity) {}
  16. // TODO: 实现移动构造器
  17. DynFibonacci(DynFibonacci && other) {
  18. };
  19. // TODO: 实现移动赋值
  20. // NOTICE: ⚠ 注意移动到自身问题 ⚠
  21. DynFibonacci &operator=(DynFibonacci && other) noexcept {
  22. // ASK AI: 这里的 noexcept 是什么含义?
  23. }
  24. // TODO: 实现正确的缓存优化斐波那契计算
  25. // 我们重载了下标运算符,请给出正确的实现
  26. size_t operator[](int i) {
  27. }
  28. // NOTICE: 不要修改这个方法
  29. // THINK: 这个方法和上面的方法有什么不同?
  30. size_t operator[](int i) const {
  31. ASSERT(i <= cached, "i out of range");
  32. return cache[i];
  33. }
  34. // NOTICE: 不要修改这个方法
  35. bool is_alive() const {
  36. return cache;
  37. }
  38. };
  39. int main(int argc, char **argv) {
  40. DynFibonacci fib(12);
  41. ASSERT(fib[10] == 55, "fibonacci(10) should be 55");
  42. DynFibonacci const fib_ = std::move(fib);
  43. ASSERT(!fib.is_alive(), "Object moved");
  44. ASSERT(fib_[10] == 55, "fibonacci(10) should be 55");
  45. DynFibonacci fib0(6);
  46. DynFibonacci fib1(12);
  47. fib0 = std::move(fib1);
  48. fib0 = std::move(fib0);
  49. ASSERT(fib0[10] == 55, "fibonacci(10) should be 55");
  50. return 0;
  51. }

计算机大作业