Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
cosmicing
Prj-Cpps
提交
9f9794e9
P
Prj-Cpps
项目概览
cosmicing
/
Prj-Cpps
通知
2
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
Prj-Cpps
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
9f9794e9
编写于
7月 26, 2023
作者:
cosmicing
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
add Set Map Template programs
上级
10f45341
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
124 addition
and
0 deletion
+124
-0
01-Templet/21-SetMapTemplet/.gitkeep
01-Templet/21-SetMapTemplet/.gitkeep
+0
-0
01-Templet/21-SetMapTemplet/Makefile
01-Templet/21-SetMapTemplet/Makefile
+51
-0
01-Templet/21-SetMapTemplet/Sdo.h
01-Templet/21-SetMapTemplet/Sdo.h
+49
-0
01-Templet/21-SetMapTemplet/SdoTst.cpp
01-Templet/21-SetMapTemplet/SdoTst.cpp
+24
-0
未找到文件。
01-Templet/21-SetMapTemplet/.gitkeep
0 → 100644
浏览文件 @
9f9794e9
01-Templet/21-SetMapTemplet/Makefile
0 → 100644
浏览文件 @
9f9794e9
CC
:=
g++
#注意每行后面不要有空格,否则会算到目录名里面,导致问题
SRC_DIR
=
./
BUILD_DIR
=
tmp
OBJ_DIR
=
$(BUILD_DIR)
/obj
DEPS_DIR
=
$(BUILD_DIR)
/deps
#这里添加其他头文件路径
INC_DIR
=
\
-I
./include
\
-I
./src
\
#这里添加编译参数
CC_FLAGS
:=
$(INC_DIR)
-g
-std
=
c++17
LNK_FLAGS
:=
\
-L
/usr/local/lib
#这里递归遍历3级子目录
DIRS
:=
$(
shell
find
$(SRC_DIR)
-maxdepth
3
-type
d
)
#将每个子目录添加到搜索路径
VPATH
=
$(DIRS)
#查找src_dir下面包含子目录的所有cpp文件
#SOURCES = $(foreach dir, $(DIRS), $(wildcard $(dir)/*.cpp))
SOURCES
=
SdoTst.cpp
OBJS
=
$(
addprefix
$(OBJ_DIR)
/,
$(
patsubst
%.cpp,%.o,
$(
notdir
$(SOURCES)
)))
DEPS
=
$(
addprefix
$(DEPS_DIR)
/,
$(
patsubst
%.cpp,%.d,
$(
notdir
$(SOURCES)
)))
TARGET
:=
$(
patsubst
%.cpp, %,
$(SOURCES)
)
$(TARGET)
:
$(OBJS)
$(CC)
$^
$(LNK_FLAGS)
-o
$@
#编译之前要创建OBJ目录,确保目录存在
$(OBJ_DIR)/%.o
:
%.cpp
if
[
!
-d
$(OBJ_DIR)
]
;
then
mkdir
-p
$(OBJ_DIR)
;
fi
;
\
$(CC)
-c
$(CC_FLAGS)
-o
$@
$<
#编译之前要创建DEPS目录,确保目录存在
#前面加@表示隐藏命令的执行打印
$(DEPS_DIR)/%.d
:
%.cpp
@
if
[
!
-d
$(DEPS_DIR)
]
;
then
mkdir
-p
$(DEPS_DIR)
;
fi
;
\
set
-e
;
rm
-f
$@
;
\
$(CC)
-MM
$(CC_FLAGS)
$<
>
$@
.
$$$$
;
\
sed
's,\($*\)\.o[ :]*,
$(OBJ_DIR)
/\1.o $@ : ,g'
<
$@
.
$$$$
>
$@
;
\
rm
-f
$@
.
$$$$
#前面加-表示忽略错误
-include
$(DEPS)
.PHONY
:
clean
clean
:
rm
-rf
$(BUILD_DIR)
$(TARGET)
01-Templet/21-SetMapTemplet/Sdo.h
0 → 100644
浏览文件 @
9f9794e9
/* DataList - template parameter should be like x0, x1, x2, x3...
* compare v against each x, to check if list contains or not contains v
*
* Usage:
* using DL = DataList<3, 6, 8, 19>;
* auto contains = DL::Contains(3); // contains = true
* auto contains = DL::Contains(4); // contains = flase
*/
constexpr
size_t
MAX_DATA_LIST
=
32
;
template
<
auto
...
vs
>
struct
DataList
{
static_assert
(
sizeof
...(
vs
)
<=
MAX_DATA_LIST
,
"DataList too much, use std::set instead"
);
template
<
typename
T
>
constexpr
static
bool
Contains
(
T
val
)
{
return
((
val
==
vs
)
||
...);
}
template
<
typename
T
>
constexpr
static
bool
NotContains
(
T
val
)
{
return
!
Contains
(
val
);
}
};
/* DataPairs - template parameter should be like x0, y0, x1, y1, x2, y2, ..., z
* if v matches one of x, return corresponding y, else return z as default
*
* Usage:
* using DP = DataPairs<1, 10, 2, 20, 0>;
* auto v = DP::ValueOf(1); // v == 10
* auto w = DP::ValueOf(3); // w == 0
*/
constexpr
size_t
MAX_DATA_PAIRS
=
16
;
template
<
auto
x
,
auto
y
,
auto
z
,
auto
...
vs
>
struct
DataPairs
{
static_assert
(
sizeof
...(
vs
)
%
2
==
0
,
"DataPairs must be odd, and >= 3"
);
static_assert
(
sizeof
...(
vs
)
<
MAX_DATA_PAIRS
*
2
,
"DataPairs too much, use std::map instead"
);
template
<
typename
T
>
constexpr
static
auto
ValueOf
(
T
val
)
{
if
constexpr
(
sizeof
...(
vs
)
==
0
)
{
return
val
==
x
?
y
:
z
;
}
else
{
return
val
==
x
?
y
:
DataPairs
<
z
,
vs
...
>::
ValueOf
(
val
);
}
}
};
01-Templet/21-SetMapTemplet/SdoTst.cpp
0 → 100644
浏览文件 @
9f9794e9
#include <iostream>
#include "Sdo.h"
using
namespace
std
;
using
DL
=
DataList
<
3
,
6
,
8
,
19
>
;
using
DP
=
DataPairs
<
1
,
10
,
2
,
20
,
0
>
;
int
main
()
{
auto
containsIs1
=
DL
::
Contains
(
3
);
// contains = true
auto
containsIs2
=
DL
::
Contains
(
4
);
// contains = flase
cout
<<
containsIs1
<<
endl
;
cout
<<
containsIs2
<<
endl
;
auto
v
=
DP
::
ValueOf
(
1
);
// v == 10
cout
<<
"v = "
<<
v
<<
endl
;
auto
w
=
DP
::
ValueOf
(
3
);
// w == 0
cout
<<
"w = "
<<
w
<<
endl
;
auto
x
=
DP
::
ValueOf
(
2
);
// x == 20
cout
<<
"x = "
<<
x
<<
endl
;
return
0
;
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录