-
Notifications
You must be signed in to change notification settings - Fork 376
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: YdrMaster <[email protected]>
- Loading branch information
Showing
9 changed files
with
202 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
#include "../exercise.h" | ||
#include <vector> | ||
|
||
// READ: std::vector <https://zh.cppreference.com/w/cpp/container/vector> | ||
|
||
// TODO: 将下列 `?` 替换为正确的代码 | ||
int main(int argc, char **argv) { | ||
{ | ||
std::vector<int> vec{1, 2, 3, 4, 5}; | ||
ASSERT(vec.size() == ?, "Fill in the correct value."); | ||
ASSERT(sizeof(vec) == ?, "Fill in the correct value."); | ||
int ans[]{1, 2, 3, 4, 5}; | ||
ASSERT(std::memcmp(vec.?, ans, sizeof(ans)) == 0, "Fill in the correct values."); | ||
} | ||
{ | ||
std::vector<double> vec{1, 2, 3, 4, 5}; | ||
{ | ||
ASSERT(vec.size() == ?, "Fill in the correct value."); | ||
ASSERT(sizeof(vec) == ?, "Fill in the correct value."); | ||
double ans[]{1, 2, 3, 4, 5}; | ||
ASSERT(std::memcmp(vec.?, ans, sizeof(ans)) == 0, "Fill in the correct values."); | ||
} | ||
{ | ||
vec.push_back(6); | ||
ASSERT(vec.size() == ?, "Fill in the correct value."); | ||
ASSERT(sizeof(vec) == ?, "Fill in the correct value."); | ||
vec.pop_back(); | ||
ASSERT(vec.size() == ?, "Fill in the correct value."); | ||
ASSERT(sizeof(vec) == ?, "Fill in the correct value."); | ||
} | ||
{ | ||
vec[4] = 6; | ||
ASSERT(vec[0] == ?, "Fill in the correct value."); | ||
ASSERT(vec[1] == ?, "Fill in the correct value."); | ||
ASSERT(vec[2] == ?, "Fill in the correct value."); | ||
ASSERT(vec[3] == ?, "Fill in the correct value."); | ||
ASSERT(vec[4] == ?, "Fill in the correct value."); | ||
} | ||
{ | ||
// THINK: `std::vector` 插入删除的时间复杂度是什么? | ||
vec.insert(?, 1.5); | ||
ASSERT((vec == std::vector<double>{1, 1.5, 2, 3, 4, 6}), "Make this assertion pass."); | ||
vec.erase(?); | ||
ASSERT((vec == std::vector<double>{1, 1.5, 2, 4, 6}), "Make this assertion pass."); | ||
} | ||
{ | ||
vec.shrink_to_fit(); | ||
ASSERT(vec.capacity() == ?, "Fill in the correct value."); | ||
vec.clear(); | ||
ASSERT(vec.empty(), "`vec` is empty now."); | ||
ASSERT(vec.size() == ?, "Fill in the correct value."); | ||
ASSERT(vec.capacity() == ?, "Fill in the correct value."); | ||
} | ||
} | ||
{ | ||
std::vector<char> vec(?, ?); // TODO: 调用正确的构造函数 | ||
ASSERT(vec[0] == 'z', "Make this assertion pass."); | ||
ASSERT(vec[47] == 'z', "Make this assertion pass."); | ||
ASSERT(vec.size() == 48, "Make this assertion pass."); | ||
ASSERT(sizeof(vec) == ?, "Fill in the correct value."); | ||
{ | ||
auto capacity = vec.capacity(); | ||
vec.resize(16); | ||
ASSERT(vec.size() == ?, "Fill in the correct value."); | ||
ASSERT(vec.capacity() == ?, "Fill in the correct identifier."); | ||
} | ||
{ | ||
vec.reserve(256); | ||
ASSERT(vec.size() == ?, "Fill in the correct value."); | ||
ASSERT(vec.capacity() == ?, "Fill in the correct value."); | ||
} | ||
{ | ||
vec.push_back('a'); | ||
vec.push_back('b'); | ||
vec.push_back('c'); | ||
vec.push_back('d'); | ||
ASSERT(vec.size() == ?, "Fill in the correct value."); | ||
ASSERT(vec.capacity() == ?, "Fill in the correct value."); | ||
ASSERT(vec[15] == ?, "Fill in the correct value."); | ||
ASSERT(vec[?] == 'a', "Fill in the correct value."); | ||
ASSERT(vec[?] == 'b', "Fill in the correct value."); | ||
ASSERT(vec[?] == 'c', "Fill in the correct value."); | ||
ASSERT(vec[?] == 'd', "Fill in the correct value."); | ||
} | ||
} | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
#include "../exercise.h" | ||
#include <vector> | ||
|
||
// READ: std::vector <https://zh.cppreference.com/w/cpp/container/vector_bool> | ||
// READ: 模板特化 <https://zh.cppreference.com/w/cpp/language/template_specialization> | ||
|
||
// TODO: 将下列 `?` 替换为正确的代码 | ||
int main(int argc, char **argv) { | ||
std::vector<bool> vec(?, ?);// TODO: 正确调用构造函数 | ||
ASSERT(vec[0], "Make this assertion pass."); | ||
ASSERT(vec[99], "Make this assertion pass."); | ||
ASSERT(vec.size() == 100, "Make this assertion pass."); | ||
ASSERT(sizeof(vec) == ?, "Fill in the correct value."); | ||
{ | ||
vec[20] = false; | ||
ASSERT(?vec[20], "Fill in `vec[20]` or `!vec[20]`."); | ||
} | ||
{ | ||
vec.push_back(false); | ||
ASSERT(vec.size() == ?, "Fill in the correct value."); | ||
ASSERT(?vec[100], "Fill in `vec[100]` or `!vec[100]`."); | ||
} | ||
{ | ||
auto ref = vec[30]; | ||
ASSERT(?ref, "Fill in `ref` or `!ref`"); | ||
ref = false; | ||
ASSERT(?ref, "Fill in `ref` or `!ref`"); | ||
ASSERT(?vec[30], "Fill in `vec[30]` or `!vec[30]`."); | ||
} | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
#include "../exercise.h" | ||
#include <vector> | ||
|
||
// 张量即多维数组。连续存储张量即逻辑结构与存储结构一致的张量。 | ||
// 通常来说,形状为 [d0, d1, ..., dn] 的张量,第 n 维是 dn 个连续的元素,第 n-1 维是 dn-1 个连续的 dn 个元素,以此类推。 | ||
// 张量的步长或跨度指的是张量每个维度上坐标 +1 时,数据指针跨过的范围。 | ||
// 因此,一个连续张量,其第 n 维的步长为 1,第 n-1 维的步长为 dn,第 n-2 维的步长为 dn*dn-1,以此类推。 | ||
// 例如,一个 2x3x4 张量,其步长为 [12, 4, 1]。 | ||
|
||
// READ: 类型别名 <https://zh.cppreference.com/w/cpp/language/type_alias> | ||
using udim = unsigned int; | ||
|
||
/// @brief 计算连续存储张量的步长 | ||
/// @param shape 张量的形状 | ||
/// @return 张量每维度的访问步长 | ||
std::vector<udim> strides(std::vector<udim> const &shape) { | ||
std::vector<udim> strides(shape.size()); | ||
// TODO: 完成函数体,根据张量形状计算张量连续存储时的步长。 | ||
// READ: 逆向迭代器 std::vector::rbegin <https://zh.cppreference.com/w/cpp/container/vector/rbegin> | ||
// 使用逆向迭代器可能可以简化代码 | ||
return strides; | ||
} | ||
|
||
// ---- 不要修改以下代码 ---- | ||
int main(int argc, char **argv) { | ||
ASSERT((strides({2, 3, 4}) == std::vector<udim>{12, 4, 1}), "Make this assertion pass."); | ||
ASSERT((strides({3, 4, 5}) == std::vector<udim>{20, 5, 1}), "Make this assertion pass."); | ||
ASSERT((strides({1, 3, 224, 224}) == std::vector<udim>{150528, 50176, 224, 1}), "Make this assertion pass."); | ||
ASSERT((strides({7, 1, 1, 1, 5}) == std::vector<udim>{5, 5, 5, 5, 1}), "Make this assertion pass."); | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
#include "../exercise.h" | ||
#include <string> | ||
|
||
int main(int argc, char **argv) { | ||
ASSERT(false, "todo!"); | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
#include "../exercise.h" | ||
#include <map> | ||
#include <unordered_map> | ||
|
||
int main(int argc, char **argv) { | ||
ASSERT(false, "todo!"); | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
#include "../exercise.h" | ||
#include <algorithm> | ||
|
||
int main(int argc, char **argv) { | ||
ASSERT(false, "todo!"); | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
#include "../exercise.h" | ||
#include <numeric> | ||
|
||
int main(int argc, char **argv) { | ||
ASSERT(false, "todo!"); | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters