Skip to content

[pull] master from parallel101:master #15

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ GNUmakefile
r000*/
.wip/
*.exe
.cache
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
26 changes: 26 additions & 0 deletions 17/itersafe/demo0.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#include <map>
#include <string>
#include <iostream>

int main() {
std::map<int, int> m = {
{1, 2},
{4, 3},
{5, 4},
{7, 5},
{10, 6},
};
//for (auto &[k, v]: m) {
//m.erase(k);
//}
//for (auto it = m.begin(); it != m.end(); ++it) {
//auto &[k, v] = *it;
//m.erase(k);
//}
for (auto it = m.begin(); it != m.end();) {
auto &[k, v] = *it;
++it;
m.erase(k);
}
return 0;
}
Binary file modified 17/slides.pptx
Binary file not shown.
3 changes: 3 additions & 0 deletions 17/testv/.tasks
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[+]
build_type=Release
build_target=demo0
16 changes: 16 additions & 0 deletions 17/testv/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
cmake_minimum_required(VERSION 3.15)

if (NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE Release)
endif()
set(CMAKE_CXX_STANDARD 17)

project(MapCourse LANGUAGES CXX)

set(DEMO_LIST
demo0
)

foreach (name IN ITEMS ${DEMO_LIST})
add_executable(${name} ${name}.cpp)
endforeach()
File renamed without changes.
2 changes: 0 additions & 2 deletions 17/xmake.lua

This file was deleted.

4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

# 课程大纲

课程分为前半段和后半段,前半段主要介绍现代 C++,后半段主要介绍并行编程与优化。
第一季课程分为前半段和后半段,前半段主要介绍现代 C++,后半段主要介绍并行编程与优化。

1. 课程安排与开发环境搭建:cmake与git入门
1. 现代C++入门:常用STL容器,RAII内存管理
Expand All @@ -40,6 +40,8 @@
1. C++在ZENO中的工程实践:从primitive说起
1. 结业典礼:总结所学知识与优秀作业点评

第二季正在绝赞连载中...

# 前置条件

硬件要求:
Expand Down
7 changes: 7 additions & 0 deletions stlseries/stl_map/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
node_modules
.DS_Store
dist
*.local
index.html
.remote-assets
components.d.ts
3 changes: 3 additions & 0 deletions stlseries/stl_map/.npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# for pnpm
shamefully-hoist=true
auto-install-peers=true
11 changes: 11 additions & 0 deletions stlseries/stl_map/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Welcome to [Slidev](https://github.com/slidevjs/slidev)!

To start the slide show:

- `npm install`
- `npm run dev`
- visit http://localhost:3030

Edit the [slides.md](./slides.md) to see the changes.

Learn more about Slidev on [documentations](https://sli.dev/).
37 changes: 37 additions & 0 deletions stlseries/stl_map/components/Counter.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<script setup lang="ts">
import { ref } from 'vue'

const props = defineProps({
count: {
default: 0,
},
})

const counter = ref(props.count)
</script>

<template>
<div flex="~" w="min" border="~ gray-400 opacity-50 rounded-md">
<button
border="r gray-400 opacity-50"
p="2"
font="mono"
outline="!none"
hover:bg="gray-400 opacity-20"
@click="counter -= 1"
>
-
</button>
<span m="auto" p="2">{{ counter }}</span>
<button
border="l gray-400 opacity-50"
p="2"
font="mono"
outline="!none"
hover:bg="gray-400 opacity-20"
@click="counter += 1"
>
+
</button>
</div>
</template>
10 changes: 10 additions & 0 deletions stlseries/stl_map/experiment/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
cmake_minimum_required(VERSION 3.18)

if (NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE Release)
endif()
set(CMAKE_CXX_STANDARD 20)

project(main LANGUAGES CXX)

add_executable(main main.cpp)
58 changes: 58 additions & 0 deletions stlseries/stl_map/experiment/cppdemangle.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#pragma once

#include <typeinfo>
#include <type_traits>
#include <string>
#if (defined(__GNUC__) || defined(__clang__)) && __has_include(<cxxabi.h>)
#include <cxxabi.h>
#include <cstdlib>
#endif

namespace _cppdemangle_details {

static std::string cppdemangle(const char *name) {
#if (defined(__GNUC__) || defined(__clang__)) && __has_include(<cxxabi.h>)
int status;
char *p = abi::__cxa_demangle(name, 0, 0, &status);
std::string s = p ? p : name;
std::free(p);
#else
std::string s = name;
#endif
return s;
}

static std::string cppdemangle(std::type_info const &type) {
return cppdemangle(type.name());
}

template <class T>
static std::string cppdemangle() {
std::string s{cppdemangle(typeid(std::remove_cv_t<std::remove_reference_t<T>>))};
if (std::is_const_v<std::remove_reference_t<T>>)
s += " const";
if (std::is_volatile_v<std::remove_reference_t<T>>)
s += " volatile";
if (std::is_lvalue_reference_v<T>)
s += " &";
if (std::is_rvalue_reference_v<T>)
s += " &&";
return s;
}

}

using _cppdemangle_details::cppdemangle;

// Usage:
//
// cppdemangle<int>()
// => "int"
//
// int i;
// cppdemangle<decltype(i)>()
// => "int"
//
// int i;
// cppdemangle<decltype(std::as_const(i))>()
// => "int const &"
17 changes: 17 additions & 0 deletions stlseries/stl_map/experiment/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#include <bits/stdc++.h>
#include "print.h"
#include "cppdemangle.h"
#include "map_get.h"
using namespace std;

int main() {
map<string, int> m = {
{"answer", 42},
{"timeout", 4096},
};
m.insert({
{"timeout", 985},
{"delay", 211},
});
print(m);
}
38 changes: 38 additions & 0 deletions stlseries/stl_map/experiment/map_get.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#pragma once

#include <optional>

template <class M>
typename M::mapped_type map_get
( M const &m
, typename M::key_type const &key
, typename M::mapped_type const &defl
) {
typename M::const_iterator it = m.find(key);
if (it != m.end()) {
return it->second;
} else {
return defl;
}
}

template <class M>
std::optional<typename M::mapped_type> map_get
( M const &m
, typename M::key_type const &key
) {
typename M::const_iterator it = m.find(key);
if (it != m.end()) {
return it->second;
} else {
return std::nullopt;
}
}

// Usage:
//
// map<string, string> m;
//
// string val = map_get(m, "key").value_or("default");
//
// print(val); // "default"
Loading