Skip to content

Commit

Permalink
update: 完成 25 题
Browse files Browse the repository at this point in the history
Signed-off-by: YdrMaster <[email protected]>
  • Loading branch information
YdrMaster committed Jul 15, 2024
1 parent a2241cc commit c248b13
Show file tree
Hide file tree
Showing 9 changed files with 202 additions and 2 deletions.
88 changes: 88 additions & 0 deletions exercises/23_std_vector/main.cpp
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;
}
31 changes: 31 additions & 0 deletions exercises/24_std_vector_bool/main.cpp
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;
}
31 changes: 31 additions & 0 deletions exercises/25_strides/main.cpp
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;
}
7 changes: 7 additions & 0 deletions exercises/26_std_string/main.cpp
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;
}
8 changes: 8 additions & 0 deletions exercises/27_std_map/main.cpp
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;
}
7 changes: 7 additions & 0 deletions exercises/28_std_transform/main.cpp
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;
}
7 changes: 7 additions & 0 deletions exercises/29_std_accumulate/main.cpp
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;
}
23 changes: 22 additions & 1 deletion exercises/xmake.lua
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,25 @@ target("exercise21")
target("exercise22")
add_files("22_std_array/main.cpp")

-- TODO: vector; vector<bool>; 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;
2 changes: 1 addition & 1 deletion learn/summary.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#include <thread>
#include <vector>

constexpr auto MAX_EXERCISE = 22;
constexpr auto MAX_EXERCISE = 29;

int main(int argc, char **argv) {
if (argc == 1) {
Expand Down

0 comments on commit c248b13

Please sign in to comment.