Skip to content

Commit aaf0c66

Browse files
committed
完成27-33
1 parent 32a82e7 commit aaf0c66

File tree

7 files changed

+39
-14
lines changed

7 files changed

+39
-14
lines changed

exercises/27_strides/main.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,16 @@ std::vector<udim> strides(std::vector<udim> const &shape) {
1818
// TODO: 完成函数体,根据张量形状计算张量连续存储时的步长。
1919
// READ: 逆向迭代器 std::vector::rbegin <https://zh.cppreference.com/w/cpp/container/vector/rbegin>
2020
// 使用逆向迭代器可能可以简化代码
21+
22+
// 这里的逻辑是:从最后一个维度开始向前遍历。
23+
// 最后一个维度的步长总是 1(对于连续存储)。
24+
// 前一个维度的步长 = 后一个维度的步长 * 后一个维度的形状大小。
25+
udim current_stride = 1;
26+
for(size_t i = shape.size(); i > 0; --i){
27+
// i-1为当前维度索引
28+
strides[i - 1] = current_stride;
29+
current_stride *= shape[i - 1];
30+
}
2131
return strides;
2232
}
2333

exercises/28_std_string/main.cpp

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,18 @@ int main(int argc, char **argv) {
99
auto hello = "Hello"s;
1010
auto world = "world";
1111
// READ: `decltype` 表达式 <https://zh.cppreference.com/w/cpp/language/decltype>
12+
// `decltype`意为 "declare type"(声明类型)
13+
// - 在编译期推导一个表达式或变量的类型,但不会实际计算表达式的值。
14+
// - int a = 10;
15+
// - decltype(a) b = 20; // b 的类型是 int,因为 a 是 int
16+
// - auto add(int x, double y) -> decltype(x + y); // 推导出返回类型为 double
17+
1218
// READ: `std::is_same_v` 元编程判别 <https://zh.cppreference.com/w/cpp/types/is_same>
13-
ASSERT((std::is_same_v<decltype(hello), ?>), "Fill in the missing type.");
14-
ASSERT((std::is_same_v<decltype(world), ?>), "Fill in the missing type.");
19+
// std::is_same_v 是 C++17 引入的一个模板常量(属于 <type_traits> 头文件)。
20+
// - 用于在编译期判断两个类型是否完全相同。
21+
ASSERT((std::is_same_v<decltype(hello), std::string>), "Fill in the missing type.");
22+
ASSERT((std::is_same_v<decltype(world), const char*>), "Fill in the missing type.");
1523
// TODO: 将 `?` 替换为正确的字符串
16-
ASSERT(hello + ", " + world + '!' == "?", "Fill in the missing string.");
24+
ASSERT(hello + ", " + world + '!' == "Hello, world!", "Fill in the missing string.");
1725
return 0;
1826
}

exercises/29_std_map/main.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,13 @@
77
template<class k, class v>
88
bool key_exists(std::map<k, v> const &map, k const &key) {
99
// TODO: 实现函数
10+
return map.find(key) != map.end();
1011
}
1112

1213
template<class k, class v>
1314
void set(std::map<k, v> &map, k key, v value) {
1415
// TODO: 实现函数
16+
map[key] = value;
1517
}
1618

1719
// ---- 不要修改以下代码 ----

exercises/30_std_unique_ptr/main.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,8 @@ int main(int argc, char **argv) {
5353
{"fd"},
5454
// TODO: 分析 problems[1] 中资源的生命周期,将记录填入 `std::vector`
5555
// NOTICE: 此题结果依赖对象析构逻辑,平台相关,提交时以 CI 实际运行平台为准
56-
{"", "", "", "", "", "", "", ""},
57-
{"", "", "", "", "", "", "", ""},
56+
{"d", "ffr"},
57+
{"d", "d","r"},
5858
};
5959

6060
// ---- 不要修改以下代码 ----

exercises/31_std_shared_ptr/main.cpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,36 +10,36 @@ int main(int argc, char **argv) {
1010
std::shared_ptr<int> ptrs[]{shared, shared, shared};
1111

1212
std::weak_ptr<int> observer = shared;
13-
ASSERT(observer.use_count() == ?, "");
13+
ASSERT(observer.use_count() == 4, "");
1414

1515
ptrs[0].reset();
16-
ASSERT(observer.use_count() == ?, "");
16+
ASSERT(observer.use_count() == 3, "");
1717

1818
ptrs[1] = nullptr;
19-
ASSERT(observer.use_count() == ?, "");
19+
ASSERT(observer.use_count() == 2, "");
2020

2121
ptrs[2] = std::make_shared<int>(*shared);
22-
ASSERT(observer.use_count() == ?, "");
22+
ASSERT(observer.use_count() == 1, "");
2323

2424
ptrs[0] = shared;
2525
ptrs[1] = shared;
2626
ptrs[2] = std::move(shared);
27-
ASSERT(observer.use_count() == ?, "");
27+
ASSERT(observer.use_count() == 3, "");
2828

2929
std::ignore = std::move(ptrs[0]);
3030
ptrs[1] = std::move(ptrs[1]);
3131
ptrs[1] = std::move(ptrs[2]);
32-
ASSERT(observer.use_count() == ?, "");
32+
ASSERT(observer.use_count() == 2, "");
3333

3434
shared = observer.lock();
35-
ASSERT(observer.use_count() == ?, "");
35+
ASSERT(observer.use_count() == 3, "");
3636

3737
shared = nullptr;
3838
for (auto &ptr : ptrs) ptr = nullptr;
39-
ASSERT(observer.use_count() == ?, "");
39+
ASSERT(observer.use_count() == 0, "");
4040

4141
shared = observer.lock();
42-
ASSERT(observer.use_count() == ?, "");
42+
ASSERT(observer.use_count() == 0, "");
4343

4444
return 0;
4545
}

exercises/32_std_transform/main.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ int main(int argc, char **argv) {
1010
std::vector<int> val{8, 13, 21, 34, 55};
1111
// TODO: 调用 `std::transform`,将 `v` 中的每个元素乘以 2,并转换为字符串,存入 `ans`
1212
// std::vector<std::string> ans
13+
std::vector<std::string> ans(val.size());
14+
std::transform(val.begin(), val.end(), ans.begin(), [](int x) {
15+
return std::to_string(x * 2);
16+
});
1317
ASSERT(ans.size() == val.size(), "ans size should be equal to val size");
1418
ASSERT(ans[0] == "16", "ans[0] should be 16");
1519
ASSERT(ans[1] == "26", "ans[1] should be 26");

exercises/33_std_accumulate/main.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ int main(int argc, char **argv) {
1212
// - 连续存储;
1313
// 的张量占用的字节数
1414
// int size =
15+
int size = std::accumulate(std::begin(shape), std::end(shape), sizeof(DataType), std::multiplies<int>());
1516
ASSERT(size == 602112, "4x1x3x224x224 = 602112");
1617
return 0;
1718
}

0 commit comments

Comments
 (0)