diff --git a/exercises/23_std_vector/main.cpp b/exercises/23_std_vector/main.cpp new file mode 100644 index 00000000..c6b48172 --- /dev/null +++ b/exercises/23_std_vector/main.cpp @@ -0,0 +1,88 @@ +#include "../exercise.h" +#include + +// READ: std::vector + +// TODO: 将下列 `?` 替换为正确的代码 +int main(int argc, char **argv) { + { + std::vector 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 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{1, 1.5, 2, 3, 4, 6}), "Make this assertion pass."); + vec.erase(?); + ASSERT((vec == std::vector{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 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; +} diff --git a/exercises/24_std_vector_bool/main.cpp b/exercises/24_std_vector_bool/main.cpp new file mode 100644 index 00000000..accab4b0 --- /dev/null +++ b/exercises/24_std_vector_bool/main.cpp @@ -0,0 +1,31 @@ +#include "../exercise.h" +#include + +// READ: std::vector +// READ: 模板特化 + +// TODO: 将下列 `?` 替换为正确的代码 +int main(int argc, char **argv) { + std::vector 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; +} diff --git a/exercises/25_strides/main.cpp b/exercises/25_strides/main.cpp new file mode 100644 index 00000000..baceaf2a --- /dev/null +++ b/exercises/25_strides/main.cpp @@ -0,0 +1,31 @@ +#include "../exercise.h" +#include + +// 张量即多维数组。连续存储张量即逻辑结构与存储结构一致的张量。 +// 通常来说,形状为 [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: 类型别名 +using udim = unsigned int; + +/// @brief 计算连续存储张量的步长 +/// @param shape 张量的形状 +/// @return 张量每维度的访问步长 +std::vector strides(std::vector const &shape) { + std::vector strides(shape.size()); + // TODO: 完成函数体,根据张量形状计算张量连续存储时的步长。 + // READ: 逆向迭代器 std::vector::rbegin + // 使用逆向迭代器可能可以简化代码 + return strides; +} + +// ---- 不要修改以下代码 ---- +int main(int argc, char **argv) { + ASSERT((strides({2, 3, 4}) == std::vector{12, 4, 1}), "Make this assertion pass."); + ASSERT((strides({3, 4, 5}) == std::vector{20, 5, 1}), "Make this assertion pass."); + ASSERT((strides({1, 3, 224, 224}) == std::vector{150528, 50176, 224, 1}), "Make this assertion pass."); + ASSERT((strides({7, 1, 1, 1, 5}) == std::vector{5, 5, 5, 5, 1}), "Make this assertion pass."); + return 0; +} diff --git a/exercises/26_std_string/main.cpp b/exercises/26_std_string/main.cpp new file mode 100644 index 00000000..ce0f039b --- /dev/null +++ b/exercises/26_std_string/main.cpp @@ -0,0 +1,7 @@ +#include "../exercise.h" +#include + +int main(int argc, char **argv) { + ASSERT(false, "todo!"); + return 0; +} diff --git a/exercises/27_std_map/main.cpp b/exercises/27_std_map/main.cpp new file mode 100644 index 00000000..98a2f0b0 --- /dev/null +++ b/exercises/27_std_map/main.cpp @@ -0,0 +1,8 @@ +#include "../exercise.h" +#include +#include + +int main(int argc, char **argv) { + ASSERT(false, "todo!"); + return 0; +} diff --git a/exercises/28_std_transform/main.cpp b/exercises/28_std_transform/main.cpp new file mode 100644 index 00000000..a94bcedb --- /dev/null +++ b/exercises/28_std_transform/main.cpp @@ -0,0 +1,7 @@ +#include "../exercise.h" +#include + +int main(int argc, char **argv) { + ASSERT(false, "todo!"); + return 0; +} diff --git a/exercises/29_std_accumulate/main.cpp b/exercises/29_std_accumulate/main.cpp new file mode 100644 index 00000000..ac240036 --- /dev/null +++ b/exercises/29_std_accumulate/main.cpp @@ -0,0 +1,7 @@ +#include "../exercise.h" +#include + +int main(int argc, char **argv) { + ASSERT(false, "todo!"); + return 0; +} diff --git a/exercises/xmake.lua b/exercises/xmake.lua index d42596d2..82b2f5d9 100644 --- a/exercises/xmake.lua +++ b/exercises/xmake.lua @@ -73,4 +73,25 @@ target("exercise21") target("exercise22") add_files("22_std_array/main.cpp") --- TODO: vector; vector; deque; map; forward_list; string; transform; accumulate; fs; thread; mutex; +target("exercise23") + add_files("23_std_vector/main.cpp") + +target("exercise24") + add_files("24_std_vector_bool/main.cpp") + +target("exercise25") + add_files("25_strides/main.cpp") + +target("exercise26") + add_files("26_std_string/main.cpp") + +target("exercise27") + add_files("27_std_map/main.cpp") + +target("exercise28") + add_files("28_std_transform/main.cpp") + +target("exercise29") + add_files("29_std_accumulate/main.cpp") + +-- TODO: deque; forward_list; fs; thread; mutex; diff --git a/learn/summary.cpp b/learn/summary.cpp index fd50c0cb..94d4eeac 100644 --- a/learn/summary.cpp +++ b/learn/summary.cpp @@ -8,7 +8,7 @@ #include #include -constexpr auto MAX_EXERCISE = 22; +constexpr auto MAX_EXERCISE = 29; int main(int argc, char **argv) { if (argc == 1) {