Skip to content
This repository has been archived by the owner on Sep 15, 2024. It is now read-only.

Commit

Permalink
array: exercises
Browse files Browse the repository at this point in the history
  • Loading branch information
Sunrisepeak committed Apr 22, 2024
1 parent 19123e9 commit 12b72a5
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 79 deletions.
75 changes: 1 addition & 74 deletions exercises/array/Array.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,80 +6,7 @@
namespace d2ds {
// show your code

template <typename T, unsigned int N>
class Array {

public: // bigFive

Array() = default;

Array(const Array &dsObj) {
for (int i = 0; i < N; i++) {
mData_e[i] = dsObj.mData_e[i];
}
}

Array & operator=(const Array &dsObj) {
D2DS_SELF_ASSIGNMENT_CHECKER
for (int i = 0; i < N; i++) {
mData_e[i] = dsObj.mData_e[i];
}
return *this;
}

Array(Array &&dsObj) {
for (int i = 0; i < N; i++) {
mData_e[i] = std::move(dsObj.mData_e[i]);
}
}

Array & operator=(Array &&dsObj) {
D2DS_SELF_ASSIGNMENT_CHECKER
for (int i = 0; i < N; i++) {
mData_e[i] = std::move(dsObj.mData_e[i]);
}
return *this;
}

Array(std::initializer_list<T> list) {
int i = 0;
for (auto it = list.begin(); it != list.end() && i < N; it++) {
mData_e[i] = *it;
i++;
}
}

~Array() = default;

public:
T & operator[](int index) {
if (index < 0)
index = N + index;
return mData_e[index];
}

public:
unsigned int size() const {
return N;
}

T back() const {
return mData_e[N != 0 ? N - 1 : 0];
}

public:
int * begin() {
return mData_e;
}

int * end() {
return mData_e + N;
}

private:
T mData_e[N == 0 ? 1 : N];
};


}

#endif
4 changes: 4 additions & 0 deletions src/0_array.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
# 数组

数组在编程当中是最常用到的数据结构, 本章将会探讨关于定长数组Array和动态数组Vector核心部分的实现细节

- [定长数组Array](chapter_01_array.md)
- [动态数组Vector]()
4 changes: 2 additions & 2 deletions src/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@

# 常用数据结构
- [数组](0_array.md)
- [Array数组](chapter_01_array.md)
- [Vector动态数组]()
- [定长数组Array](chapter_01_array.md)
- [动态数组Vector]()

- [链表]()
- [嵌入式单链表]()
Expand Down
6 changes: 3 additions & 3 deletions src/chapter_01_array.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Array数组
# 定长数组Array

**预览**

Expand Down Expand Up @@ -159,7 +159,7 @@ public: // bigFive
};
```

> Note: 这里使用了 `placement new` 来构造数据结构中的对象, 他的主要功能是把内存分配和对象构造进行分离--即在已有的内存上进行构造对象。更多关于**new/delete运算符的分析**将放到[C++基础]()章节
> Note: 这里也可使用 `placement new` 来构造数据结构中的对象, 他的主要功能是把内存分配和对象构造进行分离--即在已有的内存上进行构造对象。更多关于**new/delete运算符的分析**将放到[C++基础]()章节
拷贝赋值

Expand Down Expand Up @@ -189,7 +189,7 @@ public: // bigFive
**移动语义**
有些场景为了性能会使用**移动语义**, 只去改变数据的**所有权(ownership)**来避免数据资源的重复制、频繁分配/释放带来的开销
有些场景为了性能会使用**移动语义**, 只去改变数据的**所有权**来避免数据资源的重复制、频繁分配/释放带来的开销
移动构造
Expand Down

0 comments on commit 12b72a5

Please sign in to comment.